0

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

1
  • You are confused. Whether JSON.stringify works and whether MVC will bind the result are entirely separate issues. You need to look at the request in Fiddler or Firebug and determine whether it's JSON.stringify or your MVC binder which is the problem. Commented May 13, 2011 at 16:01

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Julien :)... It worked for me... I didn't try the second option... I will try that too..
Since my class is bit complicated 2nd option did work great.. It partiall y worked... Thanks any way... For letting me know this too.. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.