【问题标题】:How to get List<Model> Object in HTTP POST如何在 HTTP POST 中获取 List<Model> 对象
【发布时间】:2010-12-29 13:23:58
【问题描述】:

我有两个模型类:

public class UserModel
{
    public Guid Id { get; set; }
    public string EmailAddress { get; set; }
    public string LoginName { get; set; }
    public string Password { get; set; }
}
public class GroupModel
{
    public Guid Id { get;  set; }
    public string Name { get; set; }
    public List<UserModel> Users { get; set; }
}

我通过继承 GroupModel 类创建了一个视图(Create.Aspx)。

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyApp.Models.GroupModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Create</title>
</head>
<body>
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Id) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Id) %>
                <%: Html.ValidationMessageFor(model => model.Id) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
</body>
</html>

它在我的 aspx 视图中只生成两个字段(Id、Name)。我还可以在ё[HttpPost]ё请求中的Model类对象中获取表单数据,其中包含控制器中ID和NAME字段的数据。

[HttpPost]
public ActionResult Create(GroupModel groupModel)
{
    try
    {
        // TODO: Add insert logic here
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

如何获取List&lt;UserModel&gt; 对象中的数据?

我正在考虑使用包含用户的复选框放置一个列表控件。然后在控制器中使用FormCollection 对象捕获它们。

请建议我的方法是否正确或有更好的方法吗?

【问题讨论】:

    标签: c# .net asp.net asp.net-mvc-3


    【解决方案1】:

    您可以使用编辑器模板 (~/Views/Shared/EditorTemplates/UserModel.ascx):

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<YourApp.Models.UserModel>" %>
    <%: Html.TextBoxFor(model => model.Id) %>
    <%: Html.ValidationMessageFor(model => model.Id) %>
    
    <%: Html.TextBoxFor(model => model.EmailAddress) %>
    <%: Html.ValidationMessageFor(model => model.EmailAddress) %>
    
    <%: Html.TextBoxFor(model => model.LoginName) %>
    <%: Html.ValidationMessageFor(model => model.LoginName) %>
    ...
    

    然后你可以在主视图中包含这个编辑器模板:

    ...
    <%: Html.EditorFor(model => model.Users) %>
    <p>
        <input type="submit" value="Create" />
    </p>
    

    这将为Users 集合中的每个项目调用编辑器模板。因此,您可以在 GET 操作中与一些用户一起填写此集合,您将能够在表单中编辑他们的信息。

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多