Im developing a website using ASP.NET MVC 5, with a multiple select using Chosen.js. My action is this:
[HttpGet]
public ActionResult Get()
{
var states = GetStates();
ViewBag.States = new SelectList(states.OrderBy(o => o.Id), "Value", "Name");
return View();
}
And in the view:
@Html.ListBoxFor(m => m.States,
ViewBag.States as SelectList, new Dictionary<string, object>
{
{"multiple", "multiple"},
{"class", "chosen-container-multi"},
{"placeholder", "State"},
{"id", "State"}
});
It works fine, but I would like to pré select the user state before load the page. Perhaps something like:
ViewBag.States = new SelectList(states.OrderBy(o => o.Id), "Value", "Name","UserStateId");
but it not works. Is there a way to do that?
I went to this approach:
Created a new string property on the model called
UserState
, then i get the user state and assign it to that property:Then on the view i simply get it: