I have the following JavaScript code that gets the Id property (Guid) from every user row in a Kendo UI grid. Now I am wondering how best to compose these Id’s and the owner roleId into a JSON object that I can pass to an MVC3 action method. Versus my silly string concat.
$("#command-add-selected").click(function () {
var json = "roleId: '51FC554E-353C-4D55-BE52-1B4BF9D2F17F', users: [";
var avail = $("#availableUsersGrid").data().kendoGrid._data;
for (var i = 0; i < avail.length; i++) {
json += "{ Id: '" + avail[i].Id + "'},";
}
json = json.slice(0, -1);
json += "]";
alert(json);
return false;
});
The action method can be GET or POST and need not return any value (this is another puzzle here, no returned view). All it does is domain updates that are fetched by other ajax code subsequent to the above code.
How can I pass the above type JSON to an action method essentially of void return type?
EDIT: This question answered the minor part of my question nicely, with how to dynamically add items to an array with push.
1.first of all u dont need to create the full json ur self use
JSON.Stringify()method to change the javascript object to JSON string.2.after u have created the JSON string u can
GETorPOSTit to any normal method in any MVC Controller of visibility public.even if the signature of the action method is like
public ActionResult MehodName(string jsonString)u can always return null.3. u can use built in
JavaScriptSerializerclass inSystem.Web.Script.Serializationnamespace to deserialize the json string u recieve in the action to create an object with the same propertieseEdit:-
make a javascript array names
usersthen inside the for loop use.push()function of javascript to insert the objects like thisEdit 2:-
so going by your previous comments u dont want that u need to deseralize the whole JSON object. going by your object architecture even if ur action method has a signature like this
and Users class like this one
and User class like this
it would automatically bind property with your complex users object