1

I've created model constains field like this

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

and view to show combobox like this

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

but I can't write a httppost method at controler class because every time I face this error

[InvalidCastException: Specified cast is not valid.] System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary2 dictionary, IEnumerable1 newContents) +131

I am reffering this article: How to write a simple Html.DropDownListFor()?

Community
  • 1
  • 1
user2019701
  • 11
  • 1
  • 2

1 Answers1

0

In view side:

@model Models.MyEggs

...

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

In Controller Action:

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

            return View(model);
        }
serefbilge
  • 1,654
  • 4
  • 29
  • 55