When I try to bind in an MVC 3 view (using an @Html.DropDownList helper) to a select list based on an IEnumerable< X >, where X is a custom class I created rather than a framework class, I get the error “DataBinding: ‘MyCustomNamespace.MyCustomClass’ does not contain a property with the name ‘MyProperty’.”. I do not get an error if I use a SelectListItem or a KeyValuePair in place of my custom class in the IEnumerable – in that case it works fine. I am guessing that the issue may be that my custom class is not known in the Html.DropDownList helper and hence can’t be accessed there? But I thought this was supposed to operate using reflection and the property names I specified during SelectList definition, so that would not be necessary… ?
Here is a simplified version of my code:
// In .cshtml file:
@Html.DropDownList("cmbSection", (SelectList)ViewBag.Section)
// In Controller:
List<MyCustomClass> filters = new List<MyCustomClass>();
MyCustomClass testItem1 = new MyCustomClass { MyProperty = "AAA"};
MyCustomClass testItem2 = new MyCustomClass { MyProperty = "BBB"};
filters.Add(testItem1);
filters.Add(testItem2);
return new SelectList(filters, "AAA", "MyPropertyName", "MyPropertyName");
// Elsewhere:
public class MyCustomClass
{
public string MyProperty
}
Thanks!
controller
View