【问题标题】:ASP.NET MVC 2: How to contain List<> ID numbering within a "class" UI template?ASP.NET MVC 2:如何在“类”UI 模板中包含 List<> ID 编号?
【发布时间】:2010-02-21 03:23:07
【问题描述】:

编辑:我正在尝试将包含列表的单个视图模型对象绑定到表单,以便帖子映射回相同的视图模型对象。

是否有任何代码可以在 MVC 2 中有效地做到这一点?

EditorFor( m => m, "Students", "Students[n]") // wrong but sorta close
// the third parameter specifies the ID of the html control
// ideally, the "Students[n]" could be a prefix for the inner fields

html 输出的位置:

<input id="Students[0]_Name" type="text" value="" />
<input id="Students[1]_Name" type="text" value="" />
<input id="Students[2]_Name" type="text" value="" />

id 程序集可能在部分“类”视图(UI 模板)中?

...

现在我想了想,我想知道这是否会在所有字段前面加上名称:

<% foreach( Student student in Model) { %><%
       EditorFor( m => student, "Student", 
                  "Students[" + Model.IndexOf(student) + "]" ) %><%
   } %>

奇怪...这将第三个参数字符串(例如,“StuffHere”)放在学生和姓名之间:

id="Students_StuffHere_Name" name="Students.StuffHere.Name"

【问题讨论】:

    标签: c# asp.net-mvc list templates


    【解决方案1】:

    没有很好地理解您的问题,但是使用 asp.net mvc 的 RC2,您现在可以编写类似 Html.EditorFor(m=>m.Students[i]) 的代码,如 here 所述。 问候。

    【讨论】:

    • 你找到了答案。我在哪里可以获得有关如何使用具有该语法的集合的示例?我可以看到他们使用数组,但他们可以使用列表吗?很容易切换,但我也担心他们发布时绑定回来。重点是将单个对象作为参数传递给 Action 方法(或 viewModel 对象中的对象集合)。
    • 您可以查看haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx 和hanselman.com/blog/… 之类的链接,也可以通过谷歌了解更多信息,但在帖子中为您的模型补充水分应该没有任何问题。问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2016-06-08
    • 2012-09-04
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多