Client-side MVC platforms (backbone, knockout, etc.) And validation

I am experimenting with various MVC client structures right now (mainly Backbone and Knockout). I'm trying to come up with a way to confirm. I have an MVC 4 Web API that returns only data. All the logic of the viewing engine is on the client side. Obviously, I understand that server-side validation is mandatory. This is achieved using DataAnnotations and standard ASP.NET MVC validation through model binding.

However, I ended up duplicating all the validation logic now on the client side. Is there a way to return data, but with validation logic attached to it?

+7
source share
2 answers

Blast-Dan is partially right, which means that you cannot transfer additional data to your server from the client, except for Key-Value pairs.

However, you can distribute your DataAnnotations to the client so that you do not have to rewrite the verification code. This is not trivial for complex rules, but the Html.EditorFor helpers will help you create input text fields with attached validation attributes that are easily matched using jquery validation and stop form submission. I am thinking of [Required], [Range ()] and [RegularExpression ()] data annotations, to name a few.

You can see a quick example if you just create a normal model and then add a controller to Visual Studio using the wizard to create Views for CRUD operations. You will see how the data annotations that you used in the Model class end up being displayed in the output html.

If you are creating HTML yourself without an HtmlHelper (which I think you can do with a knockout tag), you can check the DataAnnotationsModelValidatorProvider and the IClientValidatable interface starting at this link .

As you can see, this is still uncharted territory tho: /

+5
source

Not,

When transferring data to the servo paver you need to use the HTTP protocol, which you use the Post or Get method. it can only pass key value pairs. Using MVC data binding, you can bind these key value pairs to create a complex object.

However, to complete the verification, there is no way to transfer metadata or functions to C #. All validation logic must be written both on the server and in client-side scripting language to ensure proper validation

+2
source

All Articles