开发者

Using JSON stringify with MVC 1.0

I am using MV开发者_开发技巧C 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜