即使我在动作过滤器中修改了属性,ModelState也会变为false

时间:2014-10-08 16:52:56

标签: asp.net-mvc c#-4.0 asp.net-web-api

我已在以下操作过滤器本身中修改了ActionType,但仍然将模型状态错误视为"字段ActionType必须与正则表达式匹配' 1 | 2 | 3 | 4' "

我的模型属性为

[RegularExpression("1|2|3|4")]

public int ActionType { get; set; }

我的ActionType枚举是

 public enum ActionType
        {
            Add = 1,
            Update = 2,
            Delete = 3,
            Search = 4
        }

动作过滤器是

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var formData = actionContext.ActionArguments.FirstOrDefault().Value as EntityBase;
            if (formData != null)
            {
                string methodType = actionContext.Request.Method.Method;
                switch (methodType.ToUpper())
                {
                    case "POST":
                        formData.ActionType = (int)ActionType.Add;
                        break;
                    case "PUT":
                        formData.ActionType = (int)ActionType.Update;
                        break;
                    case "DELETE":
                        formData.ActionType = (int)ActionType.Delete;
                        break;
                    case "GET":
                        formData.ActionType = (int)ActionType.Search;
                        break;
                    // Your errors
                }
            }
            base.OnActionExecuting(actionContext);
        }

1 个答案:

答案 0 :(得分:0)

您是否可以更改ActionType属性上的数据注释以使用Range而不是RegularExpression ??

[Range(1, 4)]
public int ActionType { get; set; }