Using Knockoutjs and Server Validation in .NET MVC2

I am using MVC2.

What is the recommended way to check forms on the server side when using knockout?

Currently, most of my forms are in partial views that have a C # ViewModel with validation attributes. Something like that:

public class SomeThingViewModel { [Required] public string Name { get; set; } [Required] public int Number{ get; set; } } 

So, when the form is submitted to the server, I get all the model errors, and I can return the form with errors that appear with something like: <%: Html.ValidationMessageFor(m => m.Name)%> . Then it is reloaded into the element that contains the form on the main page so that the user can see the errors. This will kill any bindings that I had with the knockout form that I would suggest.

I'm not sure how to do this using knockout.

+7
source share
1 answer

It may be difficult, but everything is done right, like the wind.

First sync your view models. What you have on the client side in a knockout, you pass it to the server. Secondly, do not do server-side HTML with a knockout. Create fields that are configured on the server side and read on the client side that indicate the validity of each data field in your ViewModel.

So, if your model has a Name field, your ViewModel has a Name and Name_ValidationResult , which is an enumeration that indicates whether the Name field is valid and why it is not specified. If your server-side validation fails, set the validation result fields and send the entire ViewModel on the server side back to the client so that it can be re-installed as the client ViewModel after the request is completed. Basically, you recreate part of the ViewState ASP.NET, but you do it in a format that will work with Knockout.js

On the client side, there are error messages that are displayed only based on ValidationResult field values. Thus, you may have a message with a canned error that says: "The Name field must be set", which is displayed only if Name_ValidationResult is set to "Empty" (for example).

Basically, you actually use the MVVM template with little tweaking to be able to complete the circuit on the server.

So, you suggest adding ValidationResult fields to your C # ViewModel for each property. Then set the ValidationResult properties in my controller when I test the model is working. Then return viewmodel as JSON? so that I can update my knockout model. Does this require me to verify the correctness to some extent? Or can I use ModelState errors that I encountered? - Blancaurus

The bottom line is yes to all your questions.

In truth, I missed the fact that you used DataAnnotations for your validation, or I would mention it. You should be able to use ModelState errors to customize the validation results that you go to your knockout page.

The problem is that you use two fundamentally incompatible technologies and hope that they will play well together, and I do not think that this will work the way you hope. Something will need to be given, and I believe that the best moment for this is server-side. Drink cool knockout help and fix what you have on the server side.

+4
source

All Articles