DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CategoryTypeID'

瘦欲@ 提交于 2019-11-30 18:22:46

I faced this error. I was binding an object of the View Model:

editPanelViewModel.Panel = new SelectList(panels, "PanelId", "PanelName");

In the View, I created the ListBox like this:

@Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "PanelId", "PanelName"))

It should be like this in fact:

@Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "Value", "Text"))

You are defining your SelectList twice, in your controller as well as in your view.

Keep the view clean. Just the following would be enough in your case: @Html.DropDownListFor(model => model.CategoryTypeID, (SelectList)ViewBag.CategoryTypes)

I have to admit that DropDownListFor is quite confusing in the beginning :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!