C# 如何使用 asp.net mvc 编辑多选列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/995073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a multi-select-list be edited using asp.net mvc?
提问by Anders Juul
I'd like to edit an object like the one below. I'd like the UsersSelectedList populated with one or more Users from the UsersGrossList.
我想编辑一个像下面这样的对象。我希望用来自 UsersGrossList 的一个或多个用户填充 UsersSelectedList。
Using the standard edit-views in mvc, I get only strings and booleans (not shown below) mapped. Many of the examples I find on google utilizes early releases of the mvc framework whereas I use the official 1.0 release.
使用 mvc 中的标准编辑视图,我只得到字符串和布尔值(下面未显示)映射。我在 google 上找到的许多示例都使用了 mvc 框架的早期版本,而我使用的是官方 1.0 版本。
Any examples of the view is appreciated.
任何观点的例子都值得赞赏。
public class NewResultsState
{
public IList<User> UsersGrossList { get; set; }
public IList<User> UsersSelectedList { get; set; }
}
采纳答案by Mathias F
Use Html.ListBox in combination with IEnumerable SelectListItem
结合 IEnumerable SelectListItem 使用 Html.ListBox
View
看法
<% using (Html.BeginForm("Category", "Home",
null,
FormMethod.Post))
{ %>
<%= Html.ListBox("CategoriesSelected",Model.CategoryList )%>
<input type="submit" value="submit" name="subform" />
<% }%>
Controller/Model:
控制器/型号:
public List<CategoryInfo> GetCategoryList()
{
List<CategoryInfo> categories = new List<CategoryInfo>();
categories.Add( new CategoryInfo{ Name="Beverages", Key="Beverages"});
categories.Add( new CategoryInfo{ Name="Food", Key="Food"});
categories.Add(new CategoryInfo { Name = "Food1", Key = "Food1" });
categories.Add(new CategoryInfo { Name = "Food2", Key = "Food2" });
return categories;
}
public class ProductViewModel
{
public IEnumerable<SelectListItem> CategoryList { get; set; }
public IEnumerable<string> CategoriesSelected { get; set; }
}
public ActionResult Category(ProductViewModel model )
{
IEnumerable<SelectListItem> categoryList =
from category in GetCategoryList()
select new SelectListItem
{
Text = category.Name,
Value = category.Key,
Selected = (category.Key.StartsWith("Food"))
};
model.CategoryList = categoryList;
return View(model);
}
回答by eu-ge-ne
Assuming that User model has Id and Name properties:
假设 User 模型具有 Id 和 Name 属性:
<%= Html.ListBox("users", Model.UsersGrossList.Select(
x => new SelectListItem {
Text = x.Name,
Value = x.Id,
Selected = Model.UsersSelectedList.Any(y => y.Id == x.Id)
}
) %>
Or with View Model
或使用视图模型
public class ViewModel {
public Model YourModel;
public IEnumerable<SelectListItem> Users;
}
Controller:
控制器:
var usersGrossList = ...
var model = ...
var viewModel = new ViewModel {
YourModel = model;
Users = usersGrossList.Select(
x => new SelectListItem {
Text = x.Name,
Value = x.Id,
Selected = model.UsersSelectedList.Any(y => y.Id == x.Id)
}
}
View:
看法:
<%= Html.ListBox("users", Model.Users ) %>
回答by Lovely Bananas
@ eu-ge-ne < thankyou so much for your answer - was having real trouble finding a way to multiselect a list of values from and to model. Using your code I used the ListBoxFor Html control in my Edit/Update page and passed the whole model back to my controller (including the mulisple selected values) on Save.
@ eu-ge-ne < 非常感谢您的回答 - 很难找到一种方法来从模型和模型中多选值列表。使用您的代码,我在“编辑/更新”页面中使用了 ListBoxFor Html 控件,并在保存时将整个模型传回给我的控制器(包括多个选定的值)。
<%= Html.ListBoxFor(model => model, Model.UsersGrossList.Select(
x => new SelectListItem {
Text = x.Name,
Value = x.Id,
Selected = Model.UsersSelectedList.Any(y => y.Id == x.Id)
}
) %>
)%>