【问题标题】:How do I bind relational data to a model in ASP.net MVC?如何将关系数据绑定到 ASP.net MVC 中的模型?
【发布时间】:2011-02-14 23:33:10
【问题描述】:

我正在尝试为 ASP.net MVC 3 中的对象创建一个编辑器。它看起来像这样:

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.foo)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.foo)
    @Html.ValidationMessageFor(model => model.foo)
</div>
@if (Model.Items.Count > 0)
{
<table>          
    @foreach (var ii in Model.Items)
    { @Html.EditorFor(item => ii) }
</table>
}

在本例中,Items 是另一种对象的列表。问题是,当模型在视图中被编辑后发回时,Model.Items 的数据更改不存在,而数据更改为 Name 和 foo 工作。如何才能使 Items 的数据正确绑定?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    模型类:

    public class HomeControllerModel
    {
        public string Name { get; set; }
        public string foo { get; set; }
        public List<string> Items { get; set; }
    
        public HomeControllerModel()
        {
            Items = new List<string>();
        }
    }
    

    控制器类:

    public class HomeController : Controller
    {
    
    
        [HttpGet]
        public ActionResult Index()
        {
            var model = new HomeControllerModel();
            model.Name = "LukLed";
            model.foo = "bar";
            model.Items.Add("AAA");
            model.Items.Add("BBB");
            model.Items.Add("CCC");
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(HomeControllerModel model)
        {
            return View(model);
        }
    

    查看:

    @using MvcApplication4.Controllers
    @model HomeControllerModel
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <div>
            <form action="/Home/Index" method="post">
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.foo)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.foo)
                @Html.ValidationMessageFor(model => model.foo)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Items)
            </div>
            <input type="submit" value="Submit" />
            </form>
        </div>
    </body>
    </html>
    

    您不必遍历Items

    【讨论】:

    • 啊哈,就是这样!非常感谢。
    • 如果希望能够在编辑视图的列表中添加更多项目怎么办?我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多