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 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.
Any examples of the view is appreciated.
public class NewResultsState
{
public IList<User> UsersGrossList { get; set; }
public IList<User> UsersSelectedList { get; set; }
}
To implement a Multiple Select (MultiSelect) DropDownList with CheckBoxes in . NET, we will need to make use of checkbox control and apply the jQuery Bootstrap Multi-Select Plugin to it. Provide a Project nam e and confirm or change the Location. Select Create Select the latest version of ASP.NET Core in the drop-down (.
NET MVC, an advanced ListBox control with a corresponding checkbox for each item in a list. Users select or unselect multiple items by checking or unchecking the checkbox without using the shift or ctrl keys. The MultiSelectListBox control also includes filtering and a select all feature.
My guess is that most users don't know how to use a multiselect list and, without some special directions on the page, won't even know that they can make multiple selections. Really, my feeling is that, if you need multiple selections, you should be using checkboxes (which users do know how to use and instantly recognize).
1) First, fire up a project in Visual Studio for ASP.NET MVC web application. 2) Make sure the latest build of EntityFramework is added to the project. 3) Create your primary class that will be used for the parent form that will contain the dropdown list.
Assuming that User model has Id and Name properties:
<%= 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 ) %>
Use Html.ListBox in combination with IEnumerable SelectListItem
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With