[英]ASP.NET Get Value of DropDownList
I am populating a dropdownlist using this data: 我使用以下数据填充下拉列表:
return _lamb_database.Lambs().Select(lamb => new SelectListItem
{
Text = lamb.LambName,
Value = lamb.LambID.ToString()
}).ToList();
I am then passing this List to the view using a viewmodel. 然后,我使用视图模型将此列表传递给视图。 In the view I am showing the items in the viewmodel using this code:
在视图中,我使用以下代码在viewmodel中显示项目:
@Html.DropDownListFor(m => m.SelectedLamb, Model.Facilities, "Select Lamb")
SelectedLamb is an integer which gives the unique identifer for the lamb in the database. SelectedLamb是一个整数,为数据库中的羔羊提供唯一的标识符。 I am trying to pass back the unique identifier for the lamb instead of the lamb's name.
我正在尝试传回小羊的唯一标识符,而不是小羊的名字。 You can see in the above that I am trying to get this list to set SelectedLamb.
您可以在上面看到我正在尝试获取此列表以设置SelectedLamb。
I am getting this error: 我收到此错误:
The ViewData item that has the key 'SelectedLamb' is of type 'System.Int32' but must be of type 'IEnumerable'.
键为“ SelectedLamb”的ViewData项的类型为“ System.Int32”,但必须为“ IEnumerable”类型。
Does anyone know how I can get this to work? 有谁知道我该怎么做? I have spent so long on this now.
我现在已经花了很长时间了。
You need to set the Facilities
property again after submitting. 提交后,您需要再次设置
Facilities
属性。
[HttpPost]
public ActionResult YourActionName(YourModel model)
{
model.Facilities = ..; // set again
return View(model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.