ASP.Net ModelState Automatic Error for Enum List
I have a class like this
public class MyClass
{
public MyClass()
{
Enumlist = new List<MyEnum>();
}
public virtual List<MyEnum> Enumlist { get; set; }
}
and the enum is
public enum MyEnum
{
Enum1=1,
Enum2=2,
Enum3=3
}
but in my view i keep having this eror
"The value 'System.Collections.Generic.List`1[MyEnum]' is not valid for Enumlist"
I did not specify any validation attribute for the property EnumList, s开发者_如何学Co i don't why the automatic error.
Please, can someone help with this?
In Asp.Net MVC 2 it is a default behaviour of DataBinding. If you have a Date field in your model it will automatically add validation error when the binding fails for the date. Same is true for enum.
I do not know if a list of an enum type is illegal in ASP.Net MVC but i solved this problem by using enum flag attribute and bit operations on the enum to combine the enum values.
This article (http://www.codeproject.com/Articles/37921/Enums-Flags-and-Csharp-Oh-my-bad-pun.aspx) on codeproject could help anyone having this same problem.
Happy coding.
精彩评论