Blaise
Blaise

Reputation: 22212

Concerns about Ajax use in ASP.NET MVC3

I have looked into Ajax only recently (laugh at me you experts), and I feel excited about all the added functions. But there are a few concerns.

  1. Form submission

    In the Ajax examples, a json object is created either automatically (serialize the form) or manually built by retrieving val() from each DOM items. And data validation is performed using javascript. I think we can probably still use Html.EditorFor (and Html.TextboxFor etc.) to build the form. But is it possible to still use the DataAnnotation attributes added on the model/viewmodel? The MVC+Ajax examples I have seen usually don't perform any type of server side validation. Is it okay to omit that part? I guess it is fine because a user has to have javascript enabled to submit the form now. But we need some professional suggestions.

  2. View Models

    In the Ajax world, the View Models are usually expressed as a JSON object. (Please correct me if I am wrong.) Then, what is the best way to map between our domain model and the view model? Is there any tools like AutoMapper?

    Okay, I need to add something here...........

    The reason for this concern is that I have found some examples use something called Knockout.js (See its website) Instead of return Json(model) to return a json object of our view model into the $.Ajax call, its examples shows a view model is built in javascript.

    // This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
    function AppViewModel() {
        this.firstName = "Bert";
        this.lastName = "Bertington";
    }
    
    // Activates knockout.js
    ko.applyBindings(new AppViewModel());
    

    What is the benefit of such practice?

    ---- end of my update ----

Thank you for any helpful suggestions.

Upvotes: 0

Views: 123

Answers (1)

Thinking Sites
Thinking Sites

Reputation: 3542

1) Do not omit server side validation. MVC has some capabilities built in to do some of that for you on the server side, but it's a good idea to test that it's working. Normally this just tests for type, length, range, and some other basic validation. Any complex validation should be done by you. Either way TEST IT to make sure the correct validation is indeed occurring.

2) Json is most common since it works with JavaScript and is easy to serialize in .Net. I Recommend Newtonsoft.Json as your serialization library. You can use whatever language you can parse however, from protobuff to XML.

A ViewModel is a model that is sent to the view, for what the view needs and generally only goes one way, to the view.

The domain models are the objects you persist, and generally travel both ways, from client to server.

A good example might be that your view requires the current date, manager data, and employee data, so your view model contains properties for all of these, but the form only edits the employee which is a domain model that gets sent back from the client to the server to be persisted.

MVC has ModelBinders that will take your post data and turn them into whatever type you need (supposing you properly follow its conventions.) It's unlikely you'll have to map viewmodels to domain models.

Upvotes: 3

Related Questions