Microsoft Ajax ScriptManager and AspNet MVC
I’m porting a sample application from asp.net to MVC, I used this app to make my session during last DotNetMarche event dedicated to JQuery. Now I’m writing the same sample pages in asp.net mvc to compare both technologies. In an Asp.Net page I showed how to use jQuery to do Edit in place of some Photo Description in an application that manages photo album. At a certain point my jQuery script will communicate with the server through an asmx webservice exposed with a ScriptManager.
With this code I’m calling the ChangePhotoDescription method of the PhotoManager webService, as usual I pass parameters, then the two callback (success and failure) and the contex. Now that I’m in asp.net MVC all this infrastructure is completely unnecessary, first of all I write this action in the PhotoManagerController.
I declare that this action accepts only post, I inserted a sleep for demo purpose (Actually I shows that when the server needs time to answer, the ui shows a waiter gif), then I call my business logic (behind a Service) and return the result to the caller as a JSonResult. I’ve completely eliminated the asmx webservice, now here is the code to call this controller.
Thanks to jQuery I simply use the ajax method to make a call to the server, the url is composed by /ControllerName/ActionName, with the data parameter I set POST data, that will be passed to the controller action (pay attenction to the name), then I setup the various callback, and finally I insert into ajax options object a custom property called mycontext that contains the actual span I’m modifying.
Thanks to the JsonResult ASp.Net Mvc permits to call controller’s action from client code really in a breeze.
Tags: asp.net mvc jQuery