对于模型绑定复杂嵌套类型来说,什么是好的 HTML 命名约定?
如果我有一个由嵌套复杂类型组成的对象,例如一个具有 List
和 List
的 ViewModel(
public class WarViewModel
{
List<Fu> Fu { get; set; }
List<Bar> Bar { get; set; }
}
public class Fu
{
public int Id {get;set;}
public Soldier Private { get; set; }
}
public class Bar
{
public int Id {get;set;}
public Soldier Private { get; set; }
}
public class Soldier
{
public int Id {get;set;}
public string Name { get; set; }
public int Age { get; set; }
}
如果我有一个 )表单
包含2个选择
列表,带有Fu或Bar,其中每个选项
代表列表中的每个士兵,并且选项具有包含Id,Name的属性和年龄,例如
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WarViewModel>" %>
<% using (Html.BeginForm("WarAction", "War")) { %>
<% foreach (var soldier in Fu) %>
<select class="fu">
<option id='<%= soldier.Id %>' age='<%= soldier.Age %>'><%= soldier.Name %></option>
</select>
<% } %>
<% foreach (var soldier in Bar) %>
<select class="bar">
<option id='<%= soldier.Id %>' age='<%= soldier.Age %>'><%= soldier.Name %></option>
</select>
<% } %>
<% } %>
我需要使用什么命名约定,以便在提交表单时,以下操作方法的 WarViewModel 将填充在两个列表中选择的士兵?
public ActionResult WarAction(WarViewModel war)
{
return View();
}
我现在的理解是,模型绑定器不知道士兵是哪个,因此它不知道哪个 ID 与哪个士兵相关联。我有办法解决这个问题吗?我知道上面的代码无法验证,但我正在尝试为自己实现概念验证。
另外,需要改变什么才能使这种方法变得 RESTful?
If I have an object that is composed of nested complex types, e.g. a ViewModel that has a List<Fu>
and a List<Bar>
public class WarViewModel
{
List<Fu> Fu { get; set; }
List<Bar> Bar { get; set; }
}
public class Fu
{
public int Id {get;set;}
public Soldier Private { get; set; }
}
public class Bar
{
public int Id {get;set;}
public Soldier Private { get; set; }
}
public class Soldier
{
public int Id {get;set;}
public string Name { get; set; }
public int Age { get; set; }
}
if I had a form
that contained 2 select
lists, either with Fu or Bar, where each option
represented each soldier in the list, and the option had attributes containing Id, Name and Age, such as
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WarViewModel>" %>
<% using (Html.BeginForm("WarAction", "War")) { %>
<% foreach (var soldier in Fu) %>
<select class="fu">
<option id='<%= soldier.Id %>' age='<%= soldier.Age %>'><%= soldier.Name %></option>
</select>
<% } %>
<% foreach (var soldier in Bar) %>
<select class="bar">
<option id='<%= soldier.Id %>' age='<%= soldier.Age %>'><%= soldier.Name %></option>
</select>
<% } %>
<% } %>
What naming convention do I need to use so that when the form is submitted, the following action method's WarViewModel will be populated with the soldiers that are selected in both lists?
public ActionResult WarAction(WarViewModel war)
{
return View();
}
My understanding now is that the model binder won't know soldier is which, so it won't know which Id is associated with which soldier. Is there a way for me to work around this? I know that the code above won't validate but I'm trying to implement a proof of concept for myself.
Also, what needs to be changed to make this approach RESTful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试执行的操作已受支持:
http ://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
What your trying to do is already supported:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx