Every now and then we need to pass information from JavaScript to the CLR world using Ajax. There are number of methods for performing this action. In this article we will see different ways of passing JavaScript data to the CLR world.
Passing Parameters as Primitive Types:
The easiest and the most common way of passing the information to your server side method from JavaScript is to send it as individual parameters. The code below shows how to call the server side InsertCustomer method with three arguments.
The Customer JavaScript class is a simple class with three properties namely firstName, lastName and age.
The server side InsertCustomer methods is shown below:
This will work as expected and you will receive all the three arguments in your WebMethod. The problem arises when the list of arguments to pass increases. In that case you will have to increase the number of parameters for your InsertCustomer method which sounds like a bad idea. It is always a good programming practice to have minimal number of parameters for your methods. If the method is taking lots of parameters then you should always replace them with a single class instance.
The question is what parameter type should be use for InsertCustomer method. Let's try with the System.Object type and see how it behaves. The implementation below shows the updated InsertCustomer method that takes "Object" as an input parameters.
The JavaScript call also changes since now you are sending the complete object instead of the individual parameters.
If you debug the application you will notice that all the properties of the customer JavaScript object were successfully passed to the InsertCustomer method. The interesting thing to note about this object parameter is that it has the type of Dictionary<String,Object>. This is because JavaScript treats the properties as HashTable. If you like to access the properties from the C# side you can use the following code:
Since now you know that the object passed is of type Dictionary<String,Object> so you can replace the object with Dictionary<String,Object>. But instead of going down that path let's see if we can find a different way.
Creating CLR Class to Capture JavaScript Class:
We have created a CLR class called Customer with FirstName, LastName and Age properties as shown below:
We updated the InsertCustomer method to take the CLR Customer object as an argument as shown below:
The result is shown below:
You will notice that the JavaScript object populates the CLR object properties. If you check out the JavaScript proxy generated then you will find out that the JavaScript object will simply fill in any Dictionary containing any of the fields exposed by the Customer JavaScript object.
This means that we can even have a CLR object called "Order" with FirstName, LastName and Age and it will be filled with the JavaScript object properly.
Using the Dynamic Keyword in .NET 4.0:
If you are using VS2010 then you can also make use of the dynamic keyword. The dynamic keyword will end up being a Dictionary<String,Object> object and you will have to use Name,Value pair to access the values as shown below:
If you are looking for a much cleaner design then you can always have CLR classes for the sole purpose to hold the passed in JavaScript objects. This means you will have a separate class for JavaScript Object called JSCustomer which will be responsible to hold the values passed from the client side Customer object.
The properties are not case sensitive but if you want your CLR JavaScript projection object to be similar to your real JavaScript object then you can use camel casing as shown above.
Please note that in this article we used the Microsoft Ajax framework for sending Ajax request. In the next post we will check if the same behavior exists when using JQuery Ajax framework.
Conclusion:
In this article we learned different ways of passing information from JavaScript to the CLR world using Ajax. We used Microsoft Ajax to facilitate us with Ajax request. In the next article we are going to see how JQuery Ajax API behaves when sending JavaScript objects to the CLR planet.