Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 3从具有嵌套集合的视图收集/传递数据-数据在过程中丢失_C#_Asp.net Mvc 3 - Fatal编程技术网

C# ASP.NET MVC 3从具有嵌套集合的视图收集/传递数据-数据在过程中丢失

C# ASP.NET MVC 3从具有嵌套集合的视图收集/传递数据-数据在过程中丢失,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我的模型是一个简单的父/子表示,如下所示 public class Parent { public int Id { get; set; } public string Name { get; set; } public List<Child> Children { get; set; } } public class Child { public int Id { get; set; } public string Name { get;

我的模型是一个简单的父/子表示,如下所示

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Child> Children { get; set; } 
}

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
}
AddChild(parent)是一个私有函数,它处理给定父对象的持久化子对象,并返回一个包含更新子对象的父模型

这是我的问题:通话之间数据丢失。当我在[HttpGet]中创建父对象时,一切正常,我可以在调试器中看到正确设置的值,但当我在[HttpPost]中填写子对象数据并按“OK”时,父对象会带来所有值,包括我新填充的子对象,它们都是null(实际上,唯一的Id是字段,这一点我也不理解)。为什么?

我可以补充一点,如果我使用键入给孩子的视图来执行此操作,那么它就可以工作了。我真的不能只是通过一个孩子,因为我还需要跟踪/携带父母的Id。我也不明白这一点

以下是视图(首先是查看父项下的子项),此视图工作正常:

@model Parent

@{
    ViewBag.Title = "AddChild";
}
<h2>Children for parent @Model.Name </h2>
<p>
    @Html.ActionLink("Create New", "CreateChild", new {id = @Model.Id})
</p>

<table>
    <tr>
        <th>
            Name
        </th>
    <tr>
@foreach (var child in @Model.Children) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => child.Name)
        </td>
        <td>
            @Html.ActionLink("Delete", "Delete", new { id=child.Id })
        </td>
    </tr>
}
</table>

Parent.Name
为空,因为您没有发布它。您需要添加一个
@Html.TextBoxFor()
@Html.HiddenFor()
。但是您不需要它,因为您正在加载持久化的父级

对于子项,请尝试使用
For
循环

@for (var n = 0; n < Model.Children.Count; n++)
{
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model.Children[n].Name)
        </td>
    </tr>
}
for(var n=0;nModel.Children[n].Name) } 这与
ModelBinder
的工作方式有关。很清楚,
ModelBinder
是你的朋友;)


Edit:在我结束之前,我想说的是-注意助手如何使用
for
循环(与
foreach
循环相反)呈现输入
name
属性要了解
ModelBinder
需要什么。

请发布查看代码和
AddChild
@model Parent

@{
    ViewBag.Title = "AddChild";
}
<h2>Children for parent @Model.Name </h2>
<p>
    @Html.ActionLink("Create New", "CreateChild", new {id = @Model.Id})
</p>

<table>
    <tr>
        <th>
            Name
        </th>
    <tr>
@foreach (var child in @Model.Children) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => child.Name)
        </td>
        <td>
            @Html.ActionLink("Delete", "Delete", new { id=child.Id })
        </td>
    </tr>
}
</table>
@model Parent
@{
    ViewBag.Title = "CreateChild";
}

<h2>Create new child for @Model.Name</h2>

@using (Html.BeginForm())
{ 
    <table>
    <tr>
        <td>
            Name:
        </td>
    </tr>

@foreach (var child in @Model.Children)
{
    <tr>
        <td>
            @Html.EditorFor(modelItem => child.Name)
        </td>
    </tr>
}
</table>
<input id="submit" name="submit" type="submit" value="Save" />
}
private Parent AddChild(Parent parent)
{
   var updatedParent = GetParent(int parent.Id);
   updatedParent.Children.Add(parent.Children[0]); // this obviously fails as Children is NULL
   return updatedParent;
}
@for (var n = 0; n < Model.Children.Count; n++)
{
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model.Children[n].Name)
        </td>
    </tr>
}