MVC @ Html.DropDownList在ViewBag得到错误使用的SelectList [英] MVC @Html.DropDownList Getting Error with SelectList in ViewBag
本文介绍了MVC @ Html.DropDownList在ViewBag得到错误使用的SelectList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我在控制器中创建一个列表:
I have a list which I created in the controller:
var PayList = new[] {
new ListEntry { Id = 1, Name = "" },
new ListEntry { Id = 2, Name = "Yes" },
new ListEntry { Id = 3, Name = "No" }
};
ViewBag.PayList = new SelectList(PayList, "Id", "Name");
在视图中我有以下几点:
In the view I have the following:
@Html.DropDownList("Pay", new SelectList(ViewBag.PayList,"Id","Name"))
当我尝试以显示它,它说以下内容:
数据绑定:System.Web.Mvc.SelectListItem'不包含名为ID的属性。不知道为什么,这是行不通的。
When I try to display it, it says the following: DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Id'. Not sure why this is not working.
另外我怎么默认值来选择列表中。我喜欢它默认为是。我想有办法做到从控制器做到这一点。
Also how do I default a value to the select list. I like to default it to "Yes". I thought there was a way to do do this from the controller.
推荐答案
只需使用
@Html.DropDownList("Pay", ViewBag.PayList)
在您的视图
这篇关于MVC @ Html.DropDownList在ViewBag得到错误使用的SelectList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文