I have a view with several multiselect lists which are declared like this
<div class="col-md-6">
@Html.LabelFor(model => model.Counties, htmlAttributes: new { @class = "control-label" })
@Html.ListBoxFor(model => model.Counties, new MultiSelectList(ViewBag.CountyList, "Value", "Text"), htmlAttributes: new { @class = "form-control", size = 8, tabindex = 26 })
@Html.ValidationMessageFor(model => model.Counties, "", new { @class = "text-danger" })
<span class="small">Ctrl + click to select multiple items</span>
</div>
My view model contains a declaration like this:
public virtual List<long> Counties { get; protected set; }
My action looks like this:
[HttpPost]
public ActionResult Edit(TScholarshipView model, FormCollection form)
{
if (ModelState.IsValid)
{
TScholarship scholarship = Repo.GetScholarship(model.Id);
scholarship = Mapper.Map<TScholarshipView, TScholarship>(model, scholarship);
Repo.SaveOrUpdate(scholarship, HttpContext.User.Identity.Name);
return RedirectToAction("Edit", "AdminScholarship", new { id = model.Id });
}
return View("Scholarship", model);
}
On submit I can look at the post data sent by the browser and it is sending the appropriate data to the server
...&Counties=16&Counties=34&...
When the action begins to execute the value of Counties in the model is null. However I can look at the FormCollection values and the value of form["Counties"] is "16,34". Any ideas why the binding is not occurring?
I noticed this right after I posted the question. The problem was having the setter protected. This prevented the binder from setting the value of the list.