1

我已经创建了这样的模型包含字段

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, IEnumerable1 newContents) +131

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

4

1 回答 1

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);
        }
于 2013-05-16T19:46:48.167 回答