How to return multiple variables on JsonResult method
for example i want to return this two variables:
string result = "Successed";
string ID = "32"
I know how to return only one string:
return Json("Inserted");
How to return multiple variables on JsonResult method
for example i want to return this two variables:
string result = "Successed";
string ID = "32"
I know how to return only one string:
return Json("Inserted");
On your controller use something like this:
If you are using .ajax() on your JavaScript you can use your data acessing like this:
1. Return as collection inside anonymous type This is the java script/ajax call and the complete html.
This is the controller action that returns the data I tested and it is working.
public JsonResult GetProducts(string id) { List products = new List(); List flavours = new List();
}
Let me know if you have any issue running this code. Thanks Premjeet
Return an anonymous object.
I normally do something like this:
So that I can write my javascript to always expect ajax calls to return data in a certain format.
Now you can do things like an Ajax Complete global handler that can intercept things like
Redirect
orWaitAndRetry
before the normal handler gets it, and to have a standard way of communicating additional information about the returned data that is the same across your application.In Action method :
Using new keywork
In jquery side :
You should return an object with multiple properties:
The JSON serializer will convert C# anonymous types into JSON object literals.
EDIT : As per the comment "How to get this data in client"
You can use
getJSON
from view to get this data like thisMake sure you have jQuery loaded in your view for this code to work.