填充MVC Html.DropDownList的问题 [英] Issues Populating MVC Html.DropDownList
本文介绍了填充MVC Html.DropDownList的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下字典的内容填充DropDownList:
I am trying to poulate a DropDownList with the contents of a Dictionary which is like this:
public static readonly IDictionary<string, string> VideoProviderDictionary = new Dictionary<string, string>
{
{"1", "Atlantic"},
{"2", "Blue Ridge"},
...
对于该模型,我有:
public string[] VideoProvider { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
在控制器中,我试图填充列表:
In the controller, I am trying to populate the list:
[HttpGet]
public ActionResult Register()
{
var model = new RegisterViewModel();
model.Items = new SelectList(VideoProviders.VideoProviderDictionary);
return View(model);
}
问题出在标记中,使用lambda表达式的DropDownList没有重载:
The issue is in the markup, there is no overload for DropDownList that takes a lambda expression:
@Html.DropDownList(Model -> model.Items)
我尝试使用:
@Html.DropDownListFor(model => model.Items)
但是我得到了错误:
CS1501: No overload for method 'DropDownListFor' takes 1 arguments
推荐答案
控制器:
[HttpGet]
public ActionResult Register()
{
var model = new RegisterViewModel();
Viewbag.Items = new SelectList(VideoProviders.VideoProviderDictionary);
......
......
return View(model);
}
查看:
@Html.DropDownList("VideoProvider",Viewbag.Items as SelectList)
对于强类型下拉列表,请执行以下操作:
OR For strongly typed dropdownlist do this :
@Html.DropDownListFor(model=>model.VideoProvider,Viewbag.Items as SelectList)
型号:
public string VideoProvider { get; set; } //Correct here
public IEnumerable<SelectListItem> Items { get; set; } //if you are using Viewbag to bind dropdownlist(which is a easiest and effective way in MVC) then you don't need any model property for dropdown,you can remove this property.
这篇关于填充MVC Html.DropDownList的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文