Tuesday 15 February 2011

asp.net mvc - JQuery Post data to MVC Action Method? -


All I am trying to do is pass the MVC action method to the array of strings / ints. But the data always comes back empty, what am I doing wrong?

MVC Controller

  [HTTP Post] Public Functioning MyAction (list & lt; string & gt; id) {// Do something with array // but it is empty See Return (); }   

JQuery

  $ Post ("/ MyController / MyAction", JSON.stringify (id), function () {warning ("woohoo");}, "app / jsan");   

Posting in the data action result

  ["156"]    

Try:

  ... JSON.stringify ({id: id}), ...   

I'm pretty sure that the model binder is not sure that the list / array must also be bound.

Consider:

  [HttpPost] Public action result menu (list & lt; string & gt; id, list & lt; string & gt; blah) {}   

If JSON has been passed as an array of values, then which parameter is also binded? JSAN can be more complex than FormMS submissions, so it requires a bit more definition too. For example, the following ideas will work for the previous idea.

Update / Strong>

To send Jasonson properly, the following AJAX call will be required:

  $ Ajax ({type: "post", url: "/ home / index", data: json.stringify (id), content type: "app / jason; charset = utf-8"});   

The last parameter in the post (you specified application / json ) is expected to return from the server. By default, $ The post is an encoded ( application / x-www-form-urlencoded ) contentType that appears to have worked hard in shortcut method. To set up ContentType, you must use long arm version $ .jax

No comments:

Post a Comment