In a ASP.NET MVC (Razor) project, I'm using a ListBox
with Multi Select option in a Edit View,
CONTROLLER
public ActionResult Edit(int id)
{
Post post = db.Posts.Find(id);
string selectedValues = post.Tags; //This contains Selected values list (Eg: "AA,BB")
ViewBag.Tagslist = GetTags(selectedValues.Split(','));
return View(post);
}
private MultiSelectList GetTags(string[] selectedValues)
{
var tagsQuery = from d in db.Tags
orderby d.Name
select d;
return new MultiSelectList(tagsQuery, "Name", "Name", selectedValues);
}
HTML
<div class="editor-field">
@Html.ListBox("Tags", ViewBag.Tagslist as MultiSelectList)
</div>
This loads the items (Tag List) in to ListBox
, but does not highlight the items in the Selected Values
list.
How to fix this issue?
Thanks in advance.
I suspect that your
Post
class (to which your view is strongly typed) has a property calledTags
. You also useTags
as first argument of theListBox
helper. This means that the helper will look into this property first and ignore the selected values you passed to theMultiSelectList
. So the correct way to set a selected value is the following:and in the view:
And here's a full example that should illustrate:
Also I would recommend you using view models instead of passing your domain entities to the view. So in your
PostViewModel
you will have a property calledAllTags
of typeMultiSelectList
. This way you will be able to get rid of the weak typedViewBag
.