简体   繁体   English

为什么将带有子对象列表的模型传递给控制器​​时,子对象列表没有通过?

[英]Why When passing a Model, with a list of child objects, to a Controller the list of child objects is not passed through?

I have 2 Model classes: 我有2个Model类:

public class ModelWithList 
{
    int Id { get; set; }
    string Name { get; set; }
    List<SetOfGroups> GroupSets { get; set; }
}

public class SetOfGroups
{
    List<Groups> Groups { get; set; }
}

When I submit my form all the fields are pass through from ModelWithList except for the GroupSets. 当我提交表单时,除GroupSet外,所有字段均从ModelWithList传递。

Normally I would use a @Html.HiddenFor(model => model.GroupSets) for properties that aren't passed through but this can't be done with lists of custom models. 通常我会为未传递的属性使用@Html.HiddenFor(model => model.GroupSets) ,但这不能用自定义模型列表来完成。

Suppose you have this model setup: 假设您具有以下模型设置:

public class ModelWithList {
    int Id { get; set; }
    string Name { get; set; }
    List<SetOfGroups> GroupSets { get; set; }
}

public class SetOfGroups
{
    public int GroupId { get; set; }
    public string GroupName { get; set; }
}

Then you can use a for loop to iterate GroupSets and assign its index for every numeric type/ string / DateTime (including Nullable<T> ) properties it has: 然后,您可以使用for循环来迭代GroupSets并为其具有的每个数字类型/ string / DateTime (包括Nullable<T> )属性分配其索引:

@model ModelWithList

@* other code *@

@for (int i = 0; i < Model.GroupSets.Count; i++)
{
    @Html.HiddenFor(model => model.GroupSets[i].GroupId)

    @Html.HiddenFor(model => model.GroupSets[i].GroupName)
}

Next, assume that you're moving SetOfGroups properties and create a new list: 接下来,假设您要移动SetOfGroups属性并创建一个新列表:

public class SetOfGroups
{
    List<Group> Groups { get; set; }
}

public class Group
{
    public int GroupId { get; set; }
    public string GroupName { get; set; }
}

Then you should add another for loop to bind them: 然后,您应该添加另一个for循环来绑定它们:

@model ModelWithList

@* other code *@

@for (int i = 0; i < Model.GroupSets.Count; i++)
{
    @for (int j = 0; j < Model.GroupSets[i].Groups.Count; j++)
    {
        @Html.HiddenFor(model => model.GroupSets[i].Groups[j].GroupId)

        @Html.HiddenFor(model => model.GroupSets[i].Groups[j].GroupName)
    }
}

Although it may possible to add nested lists into view like above sample, the loops to render HTML helpers will become more complex and should be avoided. 尽管可以像上面的示例一样将嵌套列表添加到视图中,但是呈现HTML助手的循环将变得更加复杂,应避免使用。

Important Note: 重要的提示:

You should not assign a HiddenFor to a List<T> like this: 不应像这样将HiddenFor分配给List<T>

@Html.HiddenFor(model => model.GroupSets)

because Razor implicitly calls ToString() method to that List<T> object, resulting in fully-qualified name of the list inserted to the value attribute and the binding will ignore it because GroupSets is not a string property: 因为Razor隐式调用那个List<T>对象的ToString()方法,导致List<T>完全限定名称插入到value属性,并且绑定将忽略它,因为GroupSets不是string属性:

<input id="GroupSets" name="GroupSets" type="hidden" value="System.Collections.Generic.List`1[ProjectNamespace.Models.SetOfGroups]">

Related issue: 相关问题:

List item inside model always null on post - Asp.net MVC 模型内的列表项在发布后始终为空-Asp.net MVC

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

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