asp.net MVC3 DropDownList的问题
请大牛介绍一下 我要给下拉框赋值,首先查内容的时候要用什么类型?视图如何赋值给下拉框?
------解决方案--------------------public static class Utility
{
public static List<SelectListItem> GetShenZhenRegions(int? selectedValue = null)
{
List<SelectListItem> list = new List<SelectListItem>();
foreach (var item in CacheHelper.GetRegionsByCityID(4403))
{
SelectListItem temp = new SelectListItem();
temp.Text = item.ItemName;
temp.Value = item.ItemValue.ToString();
if (selectedValue.HasValue && item.ItemValue == selectedValue)
{
temp.Selected = true;
}
list.Add(temp);
}
return list;
}
}
View中
@Html.DropDownListFor(x => x.RegionID, Utility.GetShenZhenRegions(Model.RegionID))
------解决方案--------------------例子:
public List<SelectListItem> GetCompanySelectList()
{
List<SelectListItem> item = new List<SelectListItem>();
item.Add(new SelectListItem { Value = "", Text = "--请选择--" });
var list = "***";
foreach (var m in list)
{
item.Add(new SelectListItem { Value = m.Id.ToString(), Text = m.N