开发者

How to do Custom Validation on Multiple Properties in ASP.NET MVC 2.0?

开发者 https://www.devze.com 2022-12-29 15:50 出处:网络
Basically, what the title says. I have several properties that combine together to really make on开发者_C百科e logical answer, and i would like to run a server-side validation code (that i write) whic

Basically, what the title says. I have several properties that combine together to really make on开发者_C百科e logical answer, and i would like to run a server-side validation code (that i write) which take these multiple fields into account and hook up to only one validation output/error message that users see on the webpage.

I looked at scott guthries method of extending an attribute and using it in yoru dataannotations declarations, but, as i can see, there is no way to declare a dataannotations-style attribute on multiple properties, and you can only place the declarations (such as [Email], [Range], [Required]) over one property :(.

any ideas on how to go about this? preferably without having to go the whole AssociateValidatorProvider route? as i found thisdifficult to grasp mainly since people never tell you 'why' they are doing what they are doing in enough detail to make me feel comfortable.

at the moment, i've tried ot create a dummy hidden field on the form with the [Required] attribute on it, and i set it to an empty string when my multi-field custom validation code predicts an error, which shoudl trigger an error message, but not even that works, because when i change the values of these fields AFTER the action is received, the modelstate isn't updated with this info so presumes submitted values, and doing an UpdateModel fails with an error. on top of these errors, i suppose it isn't the elegant way to do things. so i'm listening to anything you guys suggest.


You can add those validation messages into the ModelState errors and display them on client side using Jquery/JavaScript.

If your action method return JsonResult then return the errors as JsonResult else set the errors into ViewData and then access and display them at the client side.

You can add the custom errors into the model state as follows :

 if(your custom validation fails....)
 {
     ModelState.AddModelError("Error1", "Your Error message1");
     ModelState.AddModelError("Error2", "Your Error message2");
 }

 if (!ModelState.IsValid)
 {
     return new JsonResult() { Data = GetModelStateErrors(ModelState)};
 }

 private static Collection<string> GetModelStateErrors(ModelState modelState)
  {
       Collection<string> errors = new Collection<string>();

       foreach (var item in modelState.Values)
       {
            foreach (var item2 in item.Errors)
            {
                if (!string.IsNullOrEmpty(item2.ErrorMessage))
                {
                    errors.Add(item2.ErrorMessage);

                }
            }
       }
       return errors;
  }


You can add an validation attribute to the class:

http://msdn.microsoft.com/en-us/magazine/ee336030.aspx

However I haven't been able to test this myself.


You need to create a class level attribute. In visual studio, create a new MVC 2 project and then search for PropertiesMustMatchAttribute for a simple example of how to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消