简体   繁体   English

ASP.NET MVC 中的模型绑定嵌套列表问题

[英]Problems model binding nested list in ASP.NET MVC

I am trying to bind a view to a model which contains a list in a list.我正在尝试将视图绑定到包含列表中的列表的模型。 Naturally I would prefer to use out of the box model binding.当然,我更喜欢使用开箱即用的模型绑定。 Having spent some time on it yesterday I found a workaround which is really a hack and I would like to correct this.昨天花了一些时间,我找到了一个解决方法,这确实是一个黑客,我想纠正这个问题。 The basic structure of my models are as follows:我的模型的基本结构如下:

public class MyMPIModel
{
    public List<ScoreInfo> ScoreInfo { get; set; }
}

public class ScoreInfo 
{
    public int ScorePrefId { get; set; }
    public List<Category> Categories { get; set; }
}

public class Category
{
    public int Id;
    public string Name;
    public bool Checked;
}

The view InterestCategories.cshtml contains the following form:视图 InterestCategories.cshtml 包含以下表单:

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.ScoreInfo.Count; i++)
    {
        @Html.EditorFor(x => x.ScoreInfo[i])
    }
}

The editor template ScoreInfo.cshtml:编辑器模板 ScoreInfo.cshtml:

@Html.HiddenFor(x => x.ScorePrefId)
<div class="preferences-block">
    @for (var i = 0; i < Model.Categories.Count; i++)
    {
        @Html.EditorFor(x => x.Categories[i])
    }
</div>

Finally the editor template Category.cshtml:最后是编辑器模板 Category.cshtml:

@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
<label>
    @Html.CheckBoxFor(x => x.Checked, new { @class = "check"})
    <span>@Model.Name</span>
</label>

Inspecting the form using firebug I can see that all the hidden fields have been populated.使用 firebug 检查表单,我可以看到所有隐藏字段都已填充。 Also when I submit the form, Fiddler shows the correct data.同样,当我提交表单时,Fiddler 会显示正确的数据。 Here is a sample:这是一个示例:

ScoreInfo[0].Categories[1].Id   2
ScoreInfo[0].Categories[1].Name Managing Money
ScoreInfo[0].Categories[1].Checked  false

However, when I post to the controller, set a breakpoint and inspect the model, the list of ScoreInfo objects have been populated but the lists of Category objects inside the ScoreInfo object have not.但是,当我发布到控制器、设置断点并检查模型时,ScoreInfo 对象列表已填充,但 ScoreInfo 对象内的 Category 对象列表尚未填充。

I have the following POST action in my controller:我的控制器中有以下 POST 操作:

[HttpPost]
public ActionResult InterestCategories(MyMPIModel model, FormCollection form)
{
    ...

    //  model would not bind forcing me to populate via form collection
    for (var i = 0; i < model.ScoreInfo.Count; i++)
    {
        ...
        for (var j = 0; j < scoreInfo.Categories.Count; j++)
        {
                var category = scoreInfo.Categories[j];

                var prefix = "ScoreInfo[" + i + "].Categories[" + j + "]";

                category.Name = form[prefix + ".Name"];

                var sId = form[prefix + ".Id"];
                if (sId != null) category.Id = Int32.Parse(sId);

                var sChecked = form[prefix + ".Checked"];
                if (sChecked != null) category.Checked = sChecked.Contains("true");
        }
    }
}

You have to use Properties instead of Fields in your Category class:您必须在 Category 类中使​​用属​​性而不是字段:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM