ViewModel中的集合在回发后消失了

时间:2016-03-18 15:47:18

标签: c# asp.net-mvc collections binding

我有以下ViewModel:

public class ActivityReportViewModel
{
    public Dictionary<int, List<string>> Periods { get; set; }

    public List<Project> Projects { get; set; }

    public List<Templates> Templates { get; set; }

    public DateTime TimePeriod { get; set; }
}

public class Project
{
    public string Customer { get; set; }
    public string ProjectNumber { get; set; }
    public string ProjectDescription { get; set; }
    public bool IsSelected { get; set; }
    public int TemplateId { get; set; }
    public bool XLSX { get; set; }
    public bool PDF { get; set; }
}

我在我的控制器中填充此ViewModel,然后将其发送到我的Create视图,该视图工作正常,并且Projects属性的值都在那里。但是,当我将数据回发到服务器时,值已消失。我尝试为每个项目的所有属性提供HiddenFields无济于事。这是我的相关视图标记:

<div>
    @Html.LabelFor(model => model.Projects, htmlAttributes: new { @class = "ms-Label" })
    <ul class="ms-List" style="list-style:none;">
        @for (int x = 0; x < Model.Projects.Count; x++)
        {
            <li class="ms-ListItem">
                <span class="ms-ListItem-primaryText">@Model.Projects[x].ProjectDescription</span> 
                <span class="ms-ListItem-secondaryText">@Model.Projects[x].Customer</span> 
                <span class="ms-ListItem-tertiaryText">@Model.Projects[x].ProjectNumber</span> 
                @*<div class="ms-ListItem-selectionTarget js-toggleSelection"></div>*@
                @Html.HiddenFor(m => Model.Projects[x].IsSelected)
                @Html.HiddenFor(m => Model.Projects[x].ProjectDescription)
                @Html.HiddenFor(m => Model.Projects[x].Customer)
                @Html.HiddenFor(m => Model.Projects[x].ProjectNumber)
                @Html.HiddenFor(m => Model.Projects[x].XLSX)
                @Html.HiddenFor(m => Model.Projects[x].PDF)
                <div class="ms-Dropdown">
                    <i class="ms-Dropdown-caretDown ms-Icon ms-Icon--caretDown"></i>
                    @Html.DropDownListFor(m => m.Projects[x].TemplateId, new SelectList(Model.Templates, "Id", "Name"), new { @class = "ms-Dropdown-select" })
                </div>
                <div class="ms-ChoiceField">
                    <input id="excel+@Model.Projects[x].ProjectNumber" class="ms-ChoiceField-input" value="@Model.Projects[x].XLSX" type="checkbox">
                    <label for="excel+@Model.Projects[x].ProjectNumber" class="ms-ChoiceField-field"><span class="ms-Label is-required">Excel</span></label>
                </div>
                <div class="ms-ChoiceField">
                    <input id="pdf+@Model.Projects[x].ProjectNumber" class="ms-ChoiceField-input" value="@Model.Projects[x].PDF" type="checkbox">
                    <label for="pdf+@Model.Projects[x].ProjectNumber" class="ms-ChoiceField-field"><span class="ms-Label is-required">PDF</span></label>
                </div>
            </li>
        }
    </ul>
    <div>
        @Html.ValidationMessageFor(model => model.Projects, "", new { @class = "text-danger" })
    </div>            
</div>   

编辑:

这是我的POST动作方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ActivityReportViewModel report)
{
    using (AppContainer _db = new AppContainer())
    {

        if (ModelState.IsValid)
        {
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        return PartialView(report);
    }

}

1 个答案:

答案 0 :(得分:0)

DefaultViewModel Binder使用HTML属性name来确定要绑定回服务器的属性。我看不到输入元素上指定的name属性。请使用视图模型的属性在要回发到服务器的元素上指定name属性。

特定name属性如下。请注意,我已添加name属性值作为视图模型的属性

<input id="excel+@Model.Projects[x].ProjectNumber" class="ms-ChoiceField-input" name="@Model.Projects[x].ProjectNumber" value="@Model.Projects[x].XLSX" type="checkbox">