Reputation: 1690
I am using MVC 1.0.
I am using JSON.Stringify() from jquery plugin Json2.js to serialize the javascript objects to send to MVC action method.
I am always getting null in the action method parameters. Am I missing something.
Does this stringify works only in MVC 3.0?? Is it possible to pass javascript objects to action methods in MVC 1.0?
Following blog I referred: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
Thanks
Upvotes: 0
Views: 649
Reputation: 850
You can do something like that :
public void ActionMethod(string objectJson)
{
TheClass theObject = new JavaScriptSerializer().Deserialize<TheClass>(objectJson);
}
and on your page :
$.ajax({ url: "ActionMethod",
data: { objectJson: JSON.stringify(theObject) }
});
In MVC 2 (maybe it works in MVC 1) , if your class is not too complicated you can even do that :
public void ActionMethod(TheClass theObject)
{
}
+
$.ajax({ url: "ActionMethod",
data: theObjectInJson
});
Of course the fields in theObjectInJson must match TheClass
Upvotes: 3