我绑定了实体框架中的列表框数据。我在该列表框中选择了多个值。但是,这些价值观并未触发。该物业的数量仅为0。我使用下面的代码:
public class Sample1
{
[Key]
public int SampleId{ get; set; }
public string SampleDesc{ get; set; }
}
public class ExpModel
{
public List<Sample1> Sample{ get; set; }
}
public ActionResult Index()
{
ViewData["SampleList"] = new List<Sample1>(entity.samp);
return View();
}
@Html.ListBoxFor(model => model.Sample, new SelectList(((List<Details.Models.Sample1>)ViewData["SampleList"]), "SampleId", "SampleDesc"))
我该怎么办?请帮帮我......
答案 0 :(得分:0)
您应该将ListBoxFor帮助器绑定到属性,该属性是简单/标量值的集合,如字符串或整数:
public class Sample1
{
[Key]
public int SampleId { get; set; }
public string SampleDesc { get; set; }
}
public class ExpModel
{
public List<int> SelectedSampleIds { get; set; }
}
然后:
public ActionResult Index()
{
ViewData["SampleList"] = new List<Sample1>(entity.samp);
return View();
}
并在您看来:
@Html.ListBoxFor(
model => model.SelectedSampleIds,
new SelectList(
(List<Details.Models.Sample1>)ViewData["SampleList"],
"SampleId",
"SampleDesc"
)
)
现在,当您提交表单时,SelectedSampleIds
集合将在列表框中包含所选的ID。