对于模型绑定复杂嵌套类型来说,什么是好的 HTML 命名约定?

发布于 2024-09-19 13:42:32 字数 1666 浏览 5 评论 0原文

如果我有一个由嵌套复杂类型组成的对象,例如一个具有 ListList 的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文