2

I don't understand why in my controller's action I get null as string:

Here is ajax call:

var dict = { "A": "fake1", "B": "fake2" };
var data = { "dictionary": JSON.stringify(dict) };

$.ajax({
    url: '/MyController/MyMethod',
    data: JSON.stringify(data),
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json'
});

And action method:

[HttpPost]
public ActionResult MyMethod( string dictionary )
{
    Dictionary<string, string> usersToNotify = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>( dictionary );
    //...
}
bambi
  • 1,159
  • 2
  • 14
  • 31
  • Not sure about asp, but you may need parenthesis around the `new JavaScriptSerializer()` before trying to access from it – Paul S. Dec 14 '15 at 10:27
  • I wouldn't stringify `dict` in the `data` object. Just add `dict` to `data` and then stringify the whole lot. – Andy Dec 14 '15 at 10:29
  • Why not just have a model with `string A` and `string B` and post back to the model? - `data: { "A": "fake1", "B": "fake2" }` and delete the `contentType` option –  Dec 14 '15 at 10:30
  • Possible duplicate of [POST json dictionary](http://stackoverflow.com/questions/4710729/post-json-dictionary) – JotaBe Dec 14 '15 at 10:33
  • Could you just test to change in the ajax call the `data: JSON.stringify(data),` to `data: data,` because you don´t need to stringify the param object name – Jose Rocha Dec 14 '15 at 10:33
  • I am just tried your code it works fine. I think you need to correct url or check for other misprints. – Sam Zakhezin Dec 14 '15 at 10:34
  • @Andy, I tried and again got null as parameter. – bambi Dec 14 '15 at 10:44
  • @Stephen Muecke, I don't want model for the simplest thing. – bambi Dec 14 '15 at 10:44
  • @Jose Rocha, I tried and it didn't even come into my method. – bambi Dec 14 '15 at 10:44
  • @Sam Zakhezin, url isn't wrong, I put breakpoint at first line of method and see that the parameter is null. No idea how it works for you. – bambi Dec 14 '15 at 10:45
  • Adding this worked for me: dataType: "text". – bambi Dec 14 '15 at 10:50
  • Then just make your method `public ActionResult MyMethod(string A, string B)` And why on earth are you writing twice as much code as necessary and than claim that you dont want to create a model (which would still be less code than you have shown) –  Dec 14 '15 at 10:52
  • @StephenMuecke, don't be angry. :) I really don't understand why should I make my method public ActionResult MyMethod(string A, string B). It should receive dictionary with a lot of key-value pairs, not just two pairs as in this simplified example. And what is duplicate in code? – bambi Dec 14 '15 at 10:59

1 Answers1

0

Have you tried this?

   var dict = { "A": "fake1", "B": "fake2" };
   var data = { "dictionary": JSON.stringify(dict) };

   $.ajax({
   url: '/MyController/MyMethod',
       data: {
        dictionary:data.dictionary
       },
       type: 'POST',
       contentType: 'application/json',
       dataType: 'json'
   });
younes sofiane
  • 441
  • 1
  • 8
  • 22