MVC 4 ViewModel not being sent back to Controller

后端 未结 9 1760
感情败类
感情败类 2020-12-08 07:17

I can\'t seem to figure out how to send back the entire ViewModel to the controller to the \'Validate and Save\' function.

Here is my controller:

[H         


        
相关标签:
9条回答
  • 2020-12-08 07:43

    This isn't MVC specific. The HTML form will only post values contained within form elements inside the form. Your example is neither inside the form or in a form element (such as hidden inputs). You have to do this since MVC doesn't rely on View State. Put hidden fields inside the form:

    @Html.HiddenFor(x => x.Transaction.Time)
    // etc...
    

    Ask yourself though.. if the user isn't updating these values.. does your action method require them?

    0 讨论(0)
  • 2020-12-08 07:44

    Try to loop with the folowing statement not with FOREACH

    <table>
        @for (var i = 0; i < Model.itemlist.Count; i++)
        {
            <tr>
                <td>
                    @Html.HiddenFor(x => x.itemlist[i].Id)
                    @Html.HiddenFor(x => x.itemlist[i].Name)
                    @Html.DisplayFor(x => x.itemlist[i].Name)
                </td>
            </tr>
        }
    </table>
    
    0 讨论(0)
  • 2020-12-08 07:46

    The signature of the Send method that the form is posting to has a parameter named transaction, which seems to be confusing the model binder. Change the name of the parameter to be something not matching the name of a property on your model:

    [HttpPost]
    public ActionResult Send(BitcoinTransactionViewModel model)
    {
    }
    

    Also, remove the htmlAttributes parameter from your BeginForm call, since that's not doing anything useful. It becomes:

    @using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post))
    

    Any data coming back from the client could have been tampered with, so you should only post back the unique ID of the transaction and then retrieve any additional information about it from your data source to perform further processing. You'll also want to verify here that the user posting the data has access to the specified transaction ID since that could've been tampered with as well.

    0 讨论(0)
  • 2020-12-08 07:46

    Try Form Collections and get the value as. I think this may work.

    public ActionResult Send(FormCollection frm)
    {
        var time = frm['Transaction.Time'];
    }
    
    0 讨论(0)
  • 2020-12-08 07:49

    Put all fields inside the form

     @using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post))
    

    and make sure that the model

     BitcoinTransactionViewModel
    

    included in view or not?

    0 讨论(0)
  • 2020-12-08 07:50

    The action name to which the data will be posted should be same as the name of the action from which the data is being posted. The only difference should be that the second action where the data is bein posted should have [HttpPost] and the Posting method should serve only Get requests.

    0 讨论(0)
提交回复
热议问题