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.
Using the standard edit-views i
Use Html.ListBox in combination with IEnumerable SelectListItem
View
<% using (Html.BeginForm("Category", "Home",
null,
FormMethod.Post))
{ %>
<%= Html.ListBox("CategoriesSelected",Model.CategoryList )%>
<% }%>
Controller/Model:
public List GetCategoryList()
{
List categories = new List();
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 CategoryList { get; set; }
public IEnumerable CategoriesSelected { get; set; }
}
public ActionResult Category(ProductViewModel model )
{
IEnumerable 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);
}