asp.net MVC3 DropDownList的问题
请大牛介绍一下 我要给下拉框赋值,首先查内容的时候要用什么类型?视图如何赋值给下拉框? asp.net mvc C#
[解决办法]
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.Name });
}
return item;
}
public ActionResult A()
{
CompanyModel model = new CompanyModel();
model.CompanySelectList = GetCompanySelectList();
return View(model);
}
//View
@model ***.CompanyModel
@Html.DropDownListFor(m => m.CompanyId, Model.CompanySelectList)