开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜