Here is my Item class:
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public virtual List<Tag> Tags { get; set; }
}
Here's my GET action:
//
// GET: /Home/AddItem/
public ActionResult AddItem()
{
List<Tag> Tags = Db.Tags.ToList();
ViewBag.Tags = new SelectList(Tags, "TagId", "Name");
return View();
}
Here's my view:
@model MySite.Models.Item
@using (Html.BeginForm()) {
//...
@Html.ListBox("Tags")
//...
}
Finally, here's my POST action:
//
// POST: /Home/AddItem/
[HttpPost]
public ActionResult AddItem(Item Item)
{
//...
List<Tag> NewItemTags = Item.Tags.ToList();
//...
}
The problem is that Item.Tags.ToList() is always empty in the POST action... like the selected values of the ListBox aren't being sent at all.
Stuck here. Please help.