Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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# 绑定到模型的列表属性_C#_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 绑定到模型的列表属性

C# 绑定到模型的列表属性,c#,asp.net-mvc-4,razor,C#,Asp.net Mvc 4,Razor,我有下面的型号 public class FooContainer { public int ID { get; set; } public string Name { get; set; } public IList<Foo> Foos { get; set; } } public class Foo { public string Name {get; set; } public string Description {get; set;

我有下面的型号

public class FooContainer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Foo> Foos { get; set; }
}

public class Foo
{
    public string Name {get; set; }
    public string Description {get; set;}
}
我想创建一个视图,使用户能够CRUD Foos

现有研究

我读过以下内容:

以及以下SO文章

所以我知道如何来回传递IEnumerable,问题是我想传递一些容器,其他属性包括IEnumerable

要求
我希望能够绑定到这个复杂的模型,这样它就可以完整地传递给控制器。假设控制器只渲染视图,并在post上接收FooController模型。此外,我希望任何相关的文章或参考来查看启用此功能所需的语法


提前感谢

这会让您开始学习

您的模型:

public class FooContainer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Foo> Foos { get; set; }
}

public class Foo
{
    public string Name {get; set; }
    public string Description {get; set;}
}
你的看法:

@model DriveAway.Web.Models.FooContainer

@using(Html.BeginForm()) 
{
    <p>@Html.TextBoxFor(m => m.ID)</p>
    <p>@Html.TextBoxFor(m => m.Name)</p>   

    for (int i = 0; i < 5; i++)
    {
        <p>@Html.TextBoxFor(m => m.Foos[i].Name)</p>
        <p>@Html.TextBoxFor(m => m.Foos[i].Description)</p>
    }

    <button type="submit">Submit</button>

}

@ViewBag.Test

在视图中,我使用了一个
for循环
,限制为5。但这显然取决于你

嘿,请描述一下FooController中的一个方法。也许它能帮上忙:谷歌的“mvc4自定义绑定器”我正在研究mvc4自定义绑定器。。。它们看起来有点酷!如果这没有帮助,请在这里再次写下评论。你不应该需要定制的模型活页夹。嗨,鲁迪奥夫斯基,我不确定这是否是我想要的,我会看看。我不确定这将如何传回整个容器,看起来它将传回一个IEnumerable到控制器(我已经可以开始工作了)。我想传递复杂的模型,而不仅仅是一个列表。它会传递回整个FooContainer。我在视图中添加了其他两个属性。所以在提交时,它将从FooContainer传入ID和名称,以及Foo列表。
[HttpGet]
public ActionResult Foo()
{
    var model = new FooContainer();
    return View("Foo", model);
}

[HttpPost]
public ActionResult Foo(FooContainer model)
{
    ViewBag.Test = m.Foos[1].Name;
    return View("Foo", model);
}
@model DriveAway.Web.Models.FooContainer

@using(Html.BeginForm()) 
{
    <p>@Html.TextBoxFor(m => m.ID)</p>
    <p>@Html.TextBoxFor(m => m.Name)</p>   

    for (int i = 0; i < 5; i++)
    {
        <p>@Html.TextBoxFor(m => m.Foos[i].Name)</p>
        <p>@Html.TextBoxFor(m => m.Foos[i].Description)</p>
    }

    <button type="submit">Submit</button>

}

@ViewBag.Test
foreach (var f in m.Foos)
   if (!string.IsNullOrEmpty(f.Name) && !string.IsNullOrEmpty(f.Description))
       addToDB(f); // some method to add to to a database