ASP.NET MVC - 视图正在发布空模型

ASP.NET MVC - View is posting null model

我一直在尝试从我的角度调用 POST 操作。我看到 POST 方法参数始终为空。我做错了什么?

我的模型class:

public class Survey
{
    public int Id { get; set; }
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public int ResponseId { get; set; }
    public string ResponseText { get; set; }
    public string Comments { get; set; }
}

我的控制器方法:

[HttpPost]
    public ActionResult Publish(List<Survey> surveyToSave)
    {
        if (ModelState.IsValid)
        {
            return View("Index");
        }

        //Save logc 

        return View("Index");
    }

查看代码:

    @model List<Survey>
@using (Html.BeginForm("Publish","Home",FormMethod.Post))
{
    <div class="center-div">
        @foreach (var item in Model)
        {
                <div id=@item.QuestionId class="Question">
                    <table>
                        <tr>
                            <td>@item.QuestionText</td>
                        </tr>
                        <tr>
                            <td>
                              <div>
                                    <label>
                                        @Html.RadioButtonFor(m => m.Find(i => i.QuestionId == item.QuestionId).ResponseText, "Not sure")
                                        Not sure
                                    </label>
                                </div>

                                <div>
                                    <label>
                                        @Html.RadioButtonFor(m => m.Find(i => i.QuestionId == item.QuestionId).ResponseText, "Agree")
                                        Agree
                                    </label>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            }

        }
    </div>
    <div class="col-md-offset-2 col-md-10">
        <input type="submit"  value="Publish" class="btn btn-default" />
    </div>

}

模型中的大多数属性应该从视图中填充。想法是在 UI 中制作模型并在 post 中提交以保存。

您当前的视图代码正在为集合中的所有项目生成这样的输入元素。

<input id="ResponseText" name="ResponseText" type="radio" value="Not sure" />

当 属性 structure/names 与发布到您的方法的表单数据相匹配时,模型绑定将起作用。默认模型绑定器将查看已发布表单数据中每个项目的 name 属性值,并尝试在您使用的 class 对象中找到匹配的 属性作为方法参数并映射值。由于您的 HttpPost 操作方法参数是 List<Survey>,要使模型绑定起作用,您的输入元素名称应该是这样的

 <input name="[n].ResponseText" type="radio" value="Not sure" />

其中n为索引,从0开始到Model.Count-1

您可以将视图代码转换为使用 for 循环并创建具有以上 name 属性值的输入元素

@model List<Survey>
@using (Html.BeginForm("Publish", "Home", FormMethod.Post))
{
   <div class="center-div">
        @for (var i = 0; i < Model.Count; i++)
        {
          <div>
            <label>@Model[i].QuestionText</label>
            <label>@Html.RadioButtonFor(m => Model[i].ResponseText, "Not sure")No</label>
            <label>@Html.RadioButtonFor(m => Model[i].ResponseText, "Agree")Agree </label>
          </div>
          @Html.HiddenFor(a => Model[i].QuestionId)
        }
   </div>
   <input type="submit" value="Publish" class="btn btn-default" />
}