How to define conditional model binding in ASP.NET Core Razor Pages?

淺唱寂寞╮ 提交于 2020-01-16 08:36:32

问题


I'm trying to make a simple login form in ASP.NET Core Razor Pages, which has an email address field, a password field, a login button and a forgot-password button.

I want to utilize the inbuilt client-side and server-side validation on the text fields, such that the email field is required for both button submissions, but the password field is only required for login button submission.

Decorating the model properties with a [Required] attribute makes them mandatory for all form post handlers, where-as I'm looking for some way to have conditional model binding, either programmatically or through an attribute on model properties or handler method parameters.

Is it possible to define conditional model binding to achieve this, or is there a clean & simple alternative?


回答1:


You can achieve that by using custom required attribute . For example , different button will trigger different handler :

<div>
    <button type="submit" asp-page-handler="Login">Login</button>

    <button type="submit" asp-page-handler="ChangePassword">login</button>
</div>
  1. Register IActionContextAccessor :

    services.AddHttpContextAccessor();
    
  2. Custom required attributed :

    public class MyRequiredAttribute : RequiredAttribute 
    {
        private string _handlerName;
        public MyRequiredAttribute(string handlerName)
        {
            _handlerName = handlerName;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
    
            var httpContext = validationContext.GetRequiredService<IHttpContextAccessor>().HttpContext;
    
            var handlerName = httpContext.Request.Query["handler"].ToString();
            if (handlerName.ToLower().Equals(_handlerName.ToLower()))
            {
                return base.IsValid(value, validationContext);
    
            }
            else
            {
                return ValidationResult.Success;
            }
        }
    }
    
  3. Apply to your property :

    [BindProperty]
    [Required]
    [MinLength(6)]
    public string UserName { get; set; }
    
    [BindProperty]
    [MyRequiredAttribute("Login"), MinLength(6)]
    public string Password { get; set; }
    


来源:https://stackoverflow.com/questions/58515337/how-to-define-conditional-model-binding-in-asp-net-core-razor-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!