ASP.NET MVC 3: Model-Level Client Side or Remote Validation

I need to do some model level checking on the MVC 3 edit page. (To be specific, I need to confirm that field A or field B is populated, but not both and not both.)

I want to perform client-side validation as well as server-side validation, which means either using remote verification or implementing duplicate verification code. I `m alright too.

I read several posts stating that you copied your own model-level validation on the server side, but none of them are also involved in client-side validation. (I don't know - I'm sure someone out there can tell me if it is easy to check client-level validation using jQuery validation.)

I also read about implementing your own remote check from scratch, which you might have to do, since the Remote attribute is only a property level.

I read this question , which is identical to mine, but the only link that really is in place does not seem to say what the respondent says.

So my question is: is there a simple, relatively low-cost way to implement level checking at the server + client level with or without a remote component? And is there a good blog post or webpage where this is explained?

+4
source share
3 answers

This question has been answered here , which might be helpful? The answer is given to verify that at least one field is entered, but the principles given in the answer may be what you are looking for, and you should be able to change the answer in accordance with the required validation. The solution also offers server-side and client-side verification options, and I believe that you can use this solution as a module-level or property-level check?

In addition, there is also the following article here that details how to create your own custom check, similar to the one given in the answer I linked to.

Hope this helps.

0
source

I think Scott Kirkland's Data Annotation Extension does exactly what you want. Here is the blog post he wrote about his extensions.

The core library provides server-side validation attributes that can be used in any .NET 4.0 project (regardless of MVC). There is also an easy-to-connect client-side validation library that can be used in ASP.NET MVC 3 projects using unobtrusive jquery validation (only MVC3 is required, including javascript files).

+1
source

If you understood correctly, mvc 3 does include client-side validation by jquery. First, for server-side validation at the model level, you can override the default isValid function with your own validation rules, something like this (including multi-user):

public sealed class PropertyAAttribute : ValidationAttribute { public string propertyBAttribute { get; set; } public override bool IsValid(object value) { // Your validation rule here } } [PropertyA(propertyBAttribute = "PropertyB")] public object PropertyA {get;set;} public object PropertyB {get;set;} 

Then, to deal with the client side, you can simply use the included jquery validation function:

  var frm = $('#formData'); frm.validate(); 

This way you will have a client-side error message based on the rule that you defined in the model. Hope this is what you need :)

0
source

Source: https://habr.com/ru/post/1380613/


All Articles