无法在控制器中使用DropDownListFor编写正确的回发方法

时间:2013-01-28 21:47:51

标签: asp.net-mvc http-post dropdownlistfor

我已经像这样创建了模型constains字段

public Dictionary<int, string> Egg { get; set; }

并查看显示这样的组合框

@Html.DropDownListFor(m => m.Egg.Keys,
                     new SelectList(
                         Model.Egg, 
                         "Key", 
                         "Value"))

但我无法在控制器类中编写httppost方法,因为每次遇到此错误

  

[InvalidCastException:指定的强制转换无效。]      System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary 2 dictionary, IEnumerable 1 newContents)+131

我正在批评这篇文章: How to write a simple Html.DropDownListFor()?

1 个答案:

答案 0 :(得分:0)

在视野方面:

@model Models.MyEggs

...

@Html.DropDownListFor(m => m.Egg.Keys,
                         new SelectList(
                             Model.Egg, 
                             "Key", 
                             "Value"))

在控制器操作中:

public ActionResult Index()
        {                
            var model = new MyEggs();

            return View(model);
        }
相关问题