jQuery dropdownlist MVC posting back the form - maikelbos0/VDT GitHub Wiki
When you receive your viewmodel in a postback, the MVC framework will automatically bind the user selected values to the SelectedValues
property of your dropdownlist. This property acts completely independent from the Items
property and it is not necessary to have the items available when working with the selected values. Please note that all other properties will be reset to their default values.
public ActionResult Index() {
return View(new ExampleViewModel() {
DemoProperty = new JQueryDropdownlist() {
Items = new[] {
new JQueryDropdownlistItem() { Value = "1a", Text = "Option 1a" },
new JQueryDropdownlistItem() { Value = "1b", Text = "Option 1b" },
new JQueryDropdownlistItem() { Value = "2", Text = "Choice 2" },
new JQueryDropdownlistItem() { Value = "3", Text = "Third choice" }
},
SelectedValues = new[] { "1b", "2" },
IsMultiselect = true
}
});
}
[HttpPost]
public ActionResult Index(ExampleViewModel viewModel) {
var selectedValues = viewModel.DemoProperty.SelectedValues;
}