Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.
DropDownListFor(m => m. location_code, Model. location_type, new { @class = "form-control", @multiple = "multiple" }). location_code is an List<int> and location_type is List<SelectListItem> populated with data.
Yes, by default a multiselectlist will post through an array of the selected values.
This article has further information, including how to use strongly-typed views with a multiselectlist.
From the linked "article":
List<int> ToppingIds
.Yes, it returns an array.
View model:
public class MyViewModel
{
public int[] SelectedIds { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
Controller:
public ActionResult Index()
{
var model = new MyViewModel
{
// fetch the items from some data source
Items = Enumerable.Select(x => new SelectListItem
{
Value = x.Id,
Text = "item " + x.Id
})
};
return View(model);
}
View:
@model MyViewModel
@Html.ListBoxFor(x => x.SelectedIds, Model.Items)
In VegTableViewmodel:
public IEnumerable<MultiSelectList> Vegetables { get; set; }
In the Controller: Get vegetables list, and then pass it to the VegTableViewModel's Vegetables property.
viewmodel.Vegetables = vegetables .Select(d => new MultiSelectList(d.VegName));
In the View:
@Html.ListBoxFor(m => m.L, new MultiSelectList(Model.Vegetables.Select(d => d.Items))
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