The problem

In order to understand all benefints that JsAction brings in your coding activity, i will at first have a look of how is actually performed a simple retrieving data using ajax.

Suppose we have the simplest action method of the world, that takes no parameters and returns a string to print:

public string MyTestMethod()
{
     return "Test String";
}

Our aim is call method from javascript using Ajax and display that value.
Let's code it using jQuery:

function AjaxDisplayString()
{
   $.ajax({
        url: @Url.Action("MyTestMethod"),
        method: 'GET',
        success: function(data) { alert(data); }
   });
}

Not so bad, after all.
Now stop, and think for a moment. What may happen using this raw approach?
  1. Write ajax call every time is annoying
  2. We have no access to Razor helpers into js file, so @Url.Action is inaccesible.
  3. What if MyTestMethod is marked as HttpPost? You have to go back to the javascript code and modify the method. And if you change it again back to HttpGet? You have to change it another time.

Have we actually got solutions to this problems?
Of course yes.
  1. You can define a standart options object, change only what you need and send a simple
$.ajax(options);
  1. There are several extensions, like RazorJs, that enable you write Razor sintax into Js files (http://john.katsiotis.com/blog/razorjs---write-razor-inside-your-javascript-files)
  2. This is not so easy to gain, so you have to change it manually or found a your way.

Can we solve all these problems using a SINGLE solution?
Yes, using JsAction.

Now that you have understand the problem, you are ready to see how JsAction can handle all these in few code lines.
Next Step

Last edited Nov 24, 2011 at 7:41 PM by XVincentX, version 4

Comments

No comments yet.