2
我想設置一個listboxfor綁定到我的模型上的屬性的控件。我可以正確填充列表框,但在做出選擇後,應該包含選定項目的列表始終爲空。我究竟做錯了什麼?我花了幾個小時改變並嘗試不同的東西,但仍然無法正常工作。MVC 4 ListBoxFor不張貼選擇的項目
---- ----模型
public class ManagementModel
{
public IEnumerable<SelectListItem> AssignableEntities { get; set; }
public IEnumerable<EntityDT> AssignedTestEntities { get; set; }
}
-----在控制器獲取方法-----
[HttpGet()]
public ActionResult Index()
{
ManagementModel model = new ManagementModel();
List<SelectListItem> listItems = new List<SelectListItem>();
foreach (EntityDT entity in atomService.GetAllAssignableLocations())
{
SelectListItem item = new SelectListItem()
{
Selected = false,
Text = entity.EntityName,
Value = entity.EntityID.ToString()
};
listItems.Add(item);
}
model.AssignableEntities = listItems;
return View(model);
}
-----查看-----
@using (Html.BeginForm("SaveSetup", "Management"))
{
<div>
@Html.ListBoxFor(model => model.AssignedTestEntities, Model.AssignableEntities, new { style = "height:350px;width:175px;" })
</div>
<input type="submit" id="btnGiftManagementSubmit" value="Save" />
}
-----表格POST方法-----
[HttpPost]
[ValidateInput(false)]
public ActionResult SaveSetup(ManagementModel model)
{
The list is empty when it gets to this method
}