【问题标题】:Bind dropdown in mvc4 with class using Dictionary使用 Dictionary 将 mvc4 中的下拉列表与类绑定
【发布时间】:2015-03-29 20:06:25
【问题描述】:

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

请帮帮我

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2011-12-14
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多