Asp.net mvc 在筛选器属性中读取值并添加错误

Asp.net mvc 在筛选器属性中读取值并添加错误,asp.net-mvc,Asp.net Mvc,我具有以下ASP.NET MVC筛选器属性: public void OnActionExecuting(ActionExecutingContext context) { ControllerBase controller = context.Controller; } 在视图上,我有一个表单 @Html.TextBox("Captha"); 我的模型是: public class SignUpModel { public String Email { get; se

我具有以下ASP.NET MVC筛选器属性:

public void OnActionExecuting(ActionExecutingContext context) {
  ControllerBase controller = context.Controller;      
} 
在视图上,我有一个表单

@Html.TextBox("Captha");
我的模型是:

public class SignUpModel {
  public String Email { get; set; }
  public String Password { get; set; }
  public String Captcha { get; set; }
}
如何在“过滤器”属性中执行以下操作:

  • 获取文本框中插入的值

  • 如果没有值或特定条件为false,是否向模型状态添加错误

  • 我的模型中是否需要验证码属性

  • 谢谢,,
    Miguel

    您不需要ActionFilter来执行此操作。在模型中使用
    CompareAttribute
    验证验证码属性。向模型中添加另一个属性,并将其称为
    SessionValue
    ,然后使用
    CompareAttribute
    将为
    Captcha
    属性输入的值与
    SessionValue
    属性进行比较:

    public class SignUpModel {
        public string Email { get; set; }
        public string Password { get; set; }
        [Compare("SessionValue")]
        public string Captcha { get; set; }
        public string SessionValue { get; set; }
    }
    
    然后,在控制器操作中将
    SessionValue
    属性的值设置为会话中存储的值:

    var model = new SignUpModel();
    model.SessionValue = Session["MyValue"];
    return View(model);
    
    在您看来,您将拥有:

    @Html.HiddenFor(model => model.SessionValue)
    @Html.TextBoxFor(model => model.Captcha)
    @Html.ValidationMessageFor(model => model.Captcha)
    
    更新:

    如果不想在视图中将
    SessionValue
    作为隐藏输入,可以创建如下自定义验证属性:

    using System.ComponentModel.DataAnnotations;
    using System.Web;
    
    public class MyCustomValidationAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
                return true;
    
            string compareValue = HttpContext.Current.Session["MyValue"];
    
            return (string)value.Equals(compareValue);
        }
    }
    
    public class SignUpModel {
        public string Email { get; set; }
        public string Password { get; set; }
        [Required]
        [MyCustomValidation]
        public string Captcha { get; set; }
    }
    
    并且,在您的模型中使用它,如下所示:

    using System.ComponentModel.DataAnnotations;
    using System.Web;
    
    public class MyCustomValidationAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
                return true;
    
            string compareValue = HttpContext.Current.Session["MyValue"];
    
            return (string)value.Equals(compareValue);
        }
    }
    
    public class SignUpModel {
        public string Email { get; set; }
        public string Password { get; set; }
        [Required]
        [MyCustomValidation]
        public string Captcha { get; set; }
    }
    

    您希望在文本框中插入什么值?你的ActionFilter应该做什么?基本上,我需要读取文本框中插入的文本,并将其与会话值进行比较。如果它们不同,那么我想在模型状态中添加一个错误,使其显示在验证帮助的视图中。您可以发布您的模型吗?刚刚。。。但是我的模型中需要captcha属性吗?我不能在action属性中访问表单元素中插入的值吗?使用您的方法,我不需要将SessionValue作为视图中的隐藏输入吗?那是不安全的。。。