使用Dictionary将mvc4中的下拉列表与类绑定

本文关键字:下拉列表 绑定 Dictionary mvc4 使用 | 更新日期: 2025-02-18 01:56:36

我需要将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>)

我使用的是强类型视图@模型RealEstate.Models.listing

请帮我

使用Dictionary将mvc4中的下拉列表与类绑定

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})