MVC @Html.DropDownList - 默认值
我有一个在控制器中创建的列表:
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",2);
在视图中我有以下内容:
@Html.DropDownList("Pay", ViewBag.PayList as SelectList)
我期望上述内容默认为“是”,但没有发生(请注意,我在 ViewBag.PayList 中传递了 2。
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",2);
In the View I have the following:
@Html.DropDownList("Pay", ViewBag.PayList as SelectList)
I was expecting the above to default to Yes but did not happen(Note that I am passing 2 in the ViewBag.PayList.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码是健全的,但我认为为了使控制器的范围与您的视图适当地通信,您需要执行以下操作:
实际上......这是一篇好文章:dropdownlist 在 MVC3 Razor 中设置选定值
不要使用 ViewBag...熟悉创建 ViewModel。
The code is sound, but I think in order for the scope of the controller to appropriately communicate with your view, you need to do the:
Actually... here's a good article: dropdownlist set selected value in MVC3 Razor
Don't use the ViewBag... get familiar with creating ViewModels.