我需要将mvc 4中的下拉列表与类文件中的字典绑定。
我的班级文件是
namespace RealEstate.ViewModel
{
public class Dropdowns
{
public Dictionary<int, string> ddlCount()
{
Dictionary<Int32, string> dict = new Dictionary<int, string>();
dict.Add(0, "1");
dict.Add(1, "2");
dict.Add(2, "3");
dict.Add(3, "4");
dict.Add(4, "5");
dict.Add(5, "6");
dict.Add(6, "7");
dict.Add(7, "8");
return dict;
}
}
}
我使用viewbag为控制器传递值
public ActionResult Save()
{
Dropdowns ddl = new Dropdowns();
ViewBag.ddlcount = ddl.ddlCount();
return View();
}
并在视图中接收
@Html.DropDownListFor(model => model.type, ViewBag.ddlcount as List<SelectListItem>)
我正在使用强类型视图 @model RealEstate.Models.listing
请帮帮我
答案 0 :(得分:0)
<强> Controller.cs 强>
public ActionResult Save()
{
Dropdowns ddl = new Dropdowns();
ViewBag.ddlCount= new SelectList(ddl.ddlCount(),"Key", "Value", type);
return View();
}
<强> View.cshtml 强>
@Html.DropDownList("ddlCount", null, htmlAttributes: null})