【问题标题】:Model values returned as null on form post模型值在表单发布上返回为 null
【发布时间】:2013-09-26 07:12:31
【问题描述】:

我阅读了与同一问题相关的各种帖子,但似乎找不到解决方案。我有一个 MVC 4 应用程序,我在其中指定了一个表单

@using (Html.BeginForm("SaveFirstAmountPayable", "Policy", FormMethod.Post, 
       new Dictionary<string, object> { { "name", "frmPolicyFirstAmountPayable" }, 
                                        { "id", "frmPolicyFirstAmountPayable" } }))
{
     @Html.Kendo().HtmlHelper.TextBoxFor(m => 
                    m.FirstAmountPayable.FirstAmountPayableDescription, 
                    new Dictionary<string, object> { { "id", "txtDescription" }})

     // Some other inputs bounded to the model.
}

我有一个按钮

<button class="btn grey" type="button" id="btnSave" name="btnSave">Save</button>

还有一些 JQuery 代码会做一些验证,然后提交表单

$(formId).submit();

我的控制器中有一个断点,并且模型中传递的所有值都是空的。任何想法为什么会发生这种情况?

【问题讨论】:

    标签: c# asp.net-mvc-4 post


    【解决方案1】:

    好的,我发现了问题,希望这对将来的人有所帮助,

    我将以下代码 sn-p 添加到我的控制器中,以查看是否发现任何 ModelState 错误

     foreach (var modelState in ViewData.ModelState.Values)
     {
         foreach (var error in modelState.Errors)
         {
            // Do something with the error.
         }
     }
    

    发现模型绑定失败,因为它无法执行字符串转换。这让我仔细查看了表单中的输入字段,发现了以下内容

    @Html.Kendo().HtmlHelper.RadioButtonFor(m => m.FirstAmountPayable, 
                  "Condition", new { @class = "type-radio", @id = "chkCondition" })
    

    m => m.FirstAmountPayable 是我的模型,显然不能绑定到单选按钮。一旦我解决了这个问题,一切都会按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多