【问题标题】:How do I edit an IEnumerable<T> with ASP.NET MVC 3?如何使用 ASP.NET MVC 3 编辑 IEnumerable<T>?
【发布时间】:2011-05-13 03:22:31
【问题描述】:

给定以下类型

public class SomeValue
{
    public int Id { get; set; }
    public int Value { get; set; }
}

public class SomeModel
{
    public string SomeProp1 { get; set; }
    public string SomeProp2 { get; set; }
    public IEnumerable<SomeValue> MyData { get; set; }
}

我想为SomeModel 类型创建一个编辑表单,其中将包含SomeProp1SomeProp2 的常用文本字段,然后是一个表格,其中包含SomeModel.MyData 集合中每个SomeValue 的文本字段.

这是怎么做到的?这些值如何绑定回模型?

我目前有一个表单为每个值显示一个文本字段,但它们都具有相同的名称和相同的 ID。这显然不是有效的 HTML,并且会阻止 MVC 将值映射回来。

【问题讨论】:

    标签: asp.net-mvc .net-4.0 ienumerable asp.net-mvc-3


    【解决方案1】:

    您可以使用编辑器模板来完成。这样,框架将处理所有事情(从正确命名输入字段到正确地将值绑定回 post 操作)。

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // In the GET action populate your model somehow
            // and render the form so that the user can edit it
            var model = new SomeModel
            {
                SomeProp1 = "prop1",
                SomeProp2 = "prop1",
                MyData = new[] 
                {
                    new SomeValue { Id = 1, Value = 123 },
                    new SomeValue { Id = 2, Value = 456 },
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(SomeModel model)
        {
            // Here the model will be properly bound
            // with the values that the user modified
            // in the form so you could perform some action
            return View(model);
        }
    }
    

    查看(~/Views/Home/Index.aspx):

    <% using (Html.BeginForm()) { %>
    
        Prop1: <%= Html.TextBoxFor(x => x.SomeProp1) %><br/>
        Prop2: <%= Html.TextBoxFor(x => x.SomeProp2) %><br/>
        <%= Html.EditorFor(x => x.MyData) %><br/>
        <input type="submit" value="OK" />
    <% } %>
    

    最后是编辑器模板 (~/Views/Home/EditorTemplates/SomeValue.ascx),它将为 MyData 集合的每个元素自动调用:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.SomeValue>" %>
    <div>
        <%= Html.TextBoxFor(x => x.Id) %>
        <%= Html.TextBoxFor(x => x.Value) %>
    </div>
    

    【讨论】:

    • 嘿,我想我现在不会发布它,因为你的答案已经更好了:) 我不知道你可以轻松地绑定列表。谢谢
    • 对于 MVC3 使用 Razor 不是更好吗?
    【解决方案2】:

    IList 实现了 IEnumerable,因此您可以像这样修改您的模型:

    public class SomeModel {
        public string SomeProp1 { get; set; }
        public string SomeProp2 { get; set; }
        public IList<SomeValue> MyData { get; set; }
    }
    

    您可以使用IModelBinder 接口为您的特定模型创建活页夹。有几种方法可以做到这一点。您可以为模型创建一个EditorFor cshtml,它将遍历您的SomeValue 列表并输出适当的ID 等等。然后,在您的 ModelBinder 实现中,您将阅读您的 id 并适当地绑定它们。我可以稍后发布一个工作示例。

    【讨论】:

    • 提交表单时,ViewEngine 会负责将数据绑定回 Collection 中的正确元素吗?
    • 你知道什么是真正的奇怪...我可以发誓这个问题问了一些不同的问题...我想我同时阅读了两个问题并回复了错了...我要改变我的答案
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多