【发布时间】: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<UserModel> 对象中的数据?
我正在考虑使用包含用户的复选框放置一个列表控件。然后在控制器中使用FormCollection 对象捕获它们。
请建议我的方法是否正确或有更好的方法吗?
【问题讨论】:
标签: c# .net asp.net asp.net-mvc-3