【发布时间】:2014-05-07 07:12:28
【问题描述】:
我正在使用ASP.NET MVC,并且我有以下模型类:
public enum ListType
{
black,
white
}
public class ListAddSiteModel : ApiModel
{
[RequestParameter]
public ListType list { get; set; }
}
但它并没有按照我想要的方式工作。当我没有在请求的URL 中传递列表参数时,我的列表是black。但我希望如果列表参数不是black 或white 字符串,那么list 必须为空。是否可以编写自定义属性[IsParsable] 并将其添加到列表属性中。
public class ListAddSiteModel : ApiModel
{
[RequestParameter]
[IsParsable]
public ListType list { get; set; }
}
【问题讨论】:
-
list不能为空,因为enum不是可空类型。您应该将其定义为public ListType? list或者给 ListType 一个默认值 (ListType.none) - 您还应该将这些枚举和属性名称大写 -
@RGraham 问号是什么意思?
标签: c# asp.net-mvc custom-attributes