What is the point of the key parameter in ModelState.AddModelError in ASP.NET MVC?

后端 未结 4 1557
孤城傲影
孤城傲影 2021-02-06 20:29

I\'ve added validation checks in my controller that modify the ModelState if the validation fails.

For example:



        
4条回答
  •  暖寄归人
    2021-02-06 21:21

    Actually you can set any validation message while your form submission unsuccessful suppose you make a field in model

    [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }
    

    and while your modelState got invalid you can set error message bind with that field like as.

    ModelState.AddModelError("OldPassword", "Current Password do not match ");
    

    then your error message will be bind with field in model named "OldPassword"

提交回复
热议问题