简体   繁体   English

绑定到模型的List属性

[英]Binding to a List property of a Model

I have the following Model 我有以下型号

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;}
}

Example Controller 控制器示例

public class FooController : Controller
{
    public ActionResult Index(){
        return View(new FooContainer());
    }

    [HttpPost]
    public ActionResult Index(FooContainer model){
        //Do stuff with the model
    }
}

I want to create a view which enables the user to CRUD Foos. 我想创建一个使用户能够CRUD Foos的视图。

Existing Research 现有研究

I have read the following: 我已阅读以下内容:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx http://haacked.com/archive/2008/10/23/model-binding-to-a -list.aspx

along with the following SO articles 以及以下SO文章
MVC4 Allowing Users to Edit List Items MVC4允许用户编辑列表项
MVC4 bind model to ICollection or List in partial MVC4将模型绑定到ICollection或部分列出

So i know how to pass an IEnumerable<> back and forth, the problem is that i want to pass some container, with other attributes including an IEnumerable<> 所以我知道如何来回传递IEnumerable <>,问题是我想传递一些带有其他属性的容器, 包括 IEnumerable <>

Requirement 需求
I want to be able to bind to this complex model so that it is passed completely and in its entirety to the controller. 我希望能够绑定到这个复杂的模型,以便将其完全完整地传递给控制器​​。 Assume that the controller does nothing but render the view and receive the FooController model on post. 假设控制器除了渲染视图并在后期接收FooController模型外什么也不做。 Additionally i would like any relevant articles or references to View syntax required to enable this. 另外,我想要启用此功能所需的任何相关文章或对View语法的引用。

Thanks in advance 提前致谢

This should get you started. 这应该使您入门。

Your Models: 您的型号:

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;}
}

Your Controller Actions: 您的控制器动作:

[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);
}

Your View: 您的看法:

@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

What happens here is when you press submit, the iList will be sent to your HttpPost Foo() action and there you can do whatever you want with it. 这里发生的是,当您按Submit时,iList将被发送到您的HttpPost Foo()操作,您可以在其中执行任何操作。 In my example, it'll show whatever was input into the second Name text box. 在我的示例中,它将显示第二个“名称”文本框中输入的内容。 You can obviously loop through each value and check if filled out etc. eg 您显然可以遍历每个值并检查是否填写等。例如

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

In the view i used a for loop with a limit of 5. But this is obviously up to you. 在视图中,我使用了一个限制为5的for loop 。但这显然取决于您。

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

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