简单情况
一个目标用户有多个用户组。
我想要实现的MVC3形式编辑或创建动作选择从下拉群组下来。
控制器:
private List<User> GetAllUsers()
{
List<User> users;
users = find all users to List...
return users;
}
public ActionResult Edit(Guid Id)
{
ViewBag.UserGroups = new SelectList(GetAllUsers(), "UserId", "Name");
return View();
}
视图:
<div class="editor-label">
@Html.LabelFor(model => model.UserGroupId, "UserGroupId")
</div>
<div class="editor-field">
@Html.DropDownList(WHAT TO PUT HERE ?);
@Html.ValidationMessageFor(model => model.UserGroupId)
</div>
哦,请使用视图模型,切ViewBag的废话,让我感到恶心我每次看到它的时候。
public class UserViewModel
{
public int UserGroupId { get; set; }
public IEnumerable<SelectListItem> UserGroups { get; set; }
}
然后:
public ActionResult Edit(Guid Id)
{
var model = new UserViewModel
{
UserGroups = new SelectList(GetAllUsers(), "UserId", "Name")
}
return View(model);
}
并在视图:
@model UserViewModel
...
<div class="editor-label">
@Html.LabelFor(model => model.UserGroupId, "UserGroupId")
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.UserGroupId, Model.UserGroups);
@Html.ValidationMessageFor(model => model.UserGroupId)
</div>
</div>
但如果你想要让我恶心,保持ViewCrap:
@Html.DropDownListFor(
x => x.UserGroupId,
(IEnumerable<SelectListItem>)ViewBag.UserGroups
)
在控制器上:
ViewBag.UserGroupId = new SelectList(GetAllUsers(), "UserId", "Name");
在视图:
@Html.DropDownListFor(m => m.UserGroupId, null);
如果你想要的类型安全,而不是您可以使用此helper方法:
void SetSelectList<TModel, TProperty>(
TModel model,
Expression<Func<TModel, TProperty>> propertySelector,
IEnumerable<SelectListItem> list) {
this.ViewData[ExpressionHelper.GetExpressionText(propertySelector)] = list;
}
并使用它像这样:
SetSelectList(model, m => m.UserGroupId, new SelectList(GetAllUsers(), "UserId", "Name"));
特别提示:使用
ToDictionary
额外的类型安全创建时
SelectList
实例:
new SelectList(GetAllUsers().ToDictionary(u => u.UserId, u => u.Name), "Key", "Value")
如果你想选择多个用户组的用户,你需要ListBoxFor
。
在您的视图模型来处理所选择的项目和所有群体集合属性添加一个字符串数组。
public class UserViewModel
{
//Other properties here
public string[] SelectedGroups { get; set; }
public IEnumerable<SelectListItem> UserGroups{ get; set; }
}
在您的GET
操作方法,获取用户组的列表,并分配给UserGroups
广告载体PF UserViewModel视图模型对象。
public ActionResult CreateUser()
{
var vm = new UserViewModel();
//The below code is hardcoded for demo. you mat replace with DB data.
vm.UserGroups= new[]
{
new SelectListItem { Value = "1", Text = "Group 1" },
new SelectListItem { Value = "2", Text = "Group 2" },
new SelectListItem { Value = "3", Text = "Group 3" }
};
return View(vm);
}
现在,在你看来,这是强类型到UserViewModel
类,
@model UserViewModel
<h2>Create User </h2>
@using (Html.BeginForm())
{
@Html.ListBoxFor(s => s.SelectedGroups,
new SelectList(Model.UserGroups, "Value", "Text"))
<input type="submit" value="Save" />
}
现在,当用户发布这个表格,您将获得在选定的项目值SelectedGroups
视图模型的财产
[HttpPost]
public ActionResult CreateUser(UserViewModel model)
{
if (ModelState.IsValid)
{
string[] groups= model.SelectedGroups;
//check items now
//do your further things and follow PRG pattern as needed
}
//reload the groups again in the ViewModel before returning to the View
return View(model);
}