I am using mvc 3 razor and problem is recording the dropdownlist value in database using razor helpers:
@Html.DropDownListFor(m => m.Question, (IEnumerable<SelectListItem>)ViewBag.QuestionList)
Here, My view is using model binding. while in database the question column is of sting data type (varchar) and while running application it shows following erros after submitting form
The ViewData item that has the key 'Question' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
What should I change here to avoid erros here I have to use model binding.
You should ensure that inside the controller action that rendered this view you have populated the
ViewBag.QuestionListproperty with anIEnumerable<SelectListItem>. People usually forget to do this in their POST actions when redisplaying the same view containing this DropDown:Also make sure that the Question property on your model is a scalar type (string, integer, …) and not a complex type. If it is a complex type you need to select the corresponding scalar property to bind the selected value to: