There is no ViewData item of type 'IEnumerable' that has the key country

后端 未结 11 2227
鱼传尺愫
鱼传尺愫 2020-12-03 07:52

While binding dropdown in MVC, I always get this error: There is no ViewData item of type \'IEnumerable\' that has the key country.

<
11条回答
  •  无人及你
    2020-12-03 08:19

    If you were using DropDownListFor like this:

    @Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList)
    

    where MySelectList in the model was a property of type SelectList, this error could be thrown if the property was null.

    Avoid this by simple initializing it in constructor, like this:

    public MyModel()
    {
        MySelectList = new SelectList(new List()); // empty list of anything...
    }
    

    I know it's not the OP's case, but this might help someone like me which had the same error due to this.

提交回复
热议问题