The ViewData item that has the key 'GenderID' is of type 'System.Int32' but must be of type 'IEnumerable'

前端 未结 2 462
梦毁少年i
梦毁少年i 2021-01-24 22:18

The error occurs when i tried to Submit/Post the data... could someone please help i tried every post but they are not helping me. I am new to mvc... any help will be granted he

相关标签:
2条回答
  • 2021-01-24 22:43

    It could be a problem between the "Gender":

    [Display(Name="Gender")]
    public int GenderID { get; set; }
    
    public IEnumerable<SelectListItem> Gender { get; set; }
    

    If I remember well, MVC attempts to map the fields automatically. Try renaming the (Name="Gender") in (Name="GenderID") or IEnumerable<SelectListItem> Gender in IEnumerable<SelectListItem> GenderEnumeration

    0 讨论(0)
  • 2021-01-24 22:50

    The error means that the value of Model.Gender is null (and as a result the DropDownListFor() method expects that the first parameter is IEnumerable<SelectListItem>.

    In your case, when you submit the form and ModelState is invalid, you return the view and have not assigned a value to Model.Gender (hence it is null and the excption is thrown.

    Ensure that you re-assign the value of Model.Gender in the POST method (just as you did in the GET method) before you return the view.

    0 讨论(0)
提交回复
热议问题