如何在Razor MVC Post动作中绑定Model中的对象

时间:2016-10-10 19:59:10

标签: c# asp.net-mvc razor

我的模型中有一个列表对象,我试图在后期操作中绑定它。但是,当我发布时,列表始终为null。请帮忙解决这个问题。我有以下模型:

我的模型 vw_FormInfo.cs

public partial class vw_FormInfo
{
    ...
    public List<vw_SectionQuestion> SectionQuestion { get; set; }
}

vw_SectionQuestion.cs

public partial class vw_SectionQuestion
{
    ...
    public int SectionID { get; set; }
    public string SectionName { get; set; }
    public string SectionDesc { get; set; }
    public string Description { get; set; }
    ...
}

我的观点 Index.cshtml

@model EvalTool.Models.vw_FormInfo
    @{
    var formSections = Model.SectionQuestion.GroupBy(item => new { item.SectionID, item.SectionName, item.SectionDesc });
    };
    ...
    @using (Html.BeginForm("Edit", "FormInfo", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        ...
        @{int i = 0,j = 0; }
        <table id="dataTables-survey" class="compact table table-striped table-bordered table-hover dataTables">
        @foreach (var formSection in formSections)
        {
            <thead>
               <tr>
                 <th></th>
                 <th>@formSection.Key.SectionID @formSection.Key.SectionName</th>
                 <th>@formSection.Key.SectionDesc</th>
               </tr>
            </thead>
            <tbody>
            @foreach (var question in formSection)
            {
               <tr class="sect-quest-@i">
                 <td>@Html.EditorFor(modelItem => modelItem.SectionQuestion.ElementAt(j).Description)</td>                  
                 <td>@question.Order</td>    
                 <td>@question.Description</td>
               </tr>
                j++;
            }
            </tbody>
            i++;
        } 
        <tfoot>
            <tr>
                <td></td>
                <td></td>
                <td><input type="submit" class="btn btn-primary" value="Edit" /></td>
            </tr>
        </tfoot>
        </table>
    }   @*End Form*@

我的控制器 FormInfoController.cs

public class FormInfoController : Controller
    {
        ...
        [HttpPost]
        public ActionResult Edit([Bind(Include= "SectionQuestion")] vw_FormInfo vw_forminfo)
        {
            var s = vw_forminfo;
            var sections = vw_forminfo.SectionQuestion;     // This is ALWAYS null
            return View(vw_forminfo);
        }

        ...
    }

0 个答案:

没有答案