1

I went through dozens of answers to figure out the trick to posting data from $.ajax to a parameter in MVC 2's Controller. Here's as far as I got:

BTW this works if you use a GET, but fails as a POST. How would I fix it?

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Home/Get",
        data: {value:'9/14/2010 12:00:00 AM'},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.value);
        }
    });
});

And this is my MVC 2 Controller:

public class strange
{
    public string value { get; set; }
}

public JsonResult Get(strange o)
{   
    var b = new strange { value = "return" };
    return Json(b, JsonRequestBehavior.AllowGet);
}

Upon POST, o's "value" is null. Changing POST to GET, o's "value" is "9/14/2010 12:00:00 AM".

How do I get the POST to work with $.ajax?

Did anyone ever post a guide to getting JSON working with MVC2 data validation when returning JSON from the client? I know they had that in their MVC 2 futures a while ago.

2 Answers 2

2

The data which you send to the ASP.NET MVC Controller should not be JSON encoded. So you should just remove the line

contentType: "application/json; charset=utf-8",

from the $.ajax request and your program will work.

2
  • @Dr. Zim: with JavaScriptSerializer serializer = new JavaScriptSerializer(); MyData data = serializer.Deserialize<MyData> (encodedInput);. Then you can use $.ajax with data: {encodedInput: JSON.stringify(myData)} and everything will work OK. So you can implement different scenarios in MVC2 RTM.
    – Oleg
    Oct 24, 2010 at 19:14
  • +1 for the suggestion. Very nice alternative. I will try that next. Oct 24, 2010 at 22:56
1

You need to pass JSON to the controller, and it's looking for a strange object, all you're currently passing is a string called value, instead your data should look like this:

{ strange: { value:'9/14/2010 12:00:00 AM'} }

Notice how strange is not an object with the value property the server is looking for. But, it'll expect this as a string, so just use JSON.stringify() (use JSON2 if needed for other browsers, e.g. < IE8):

data: JSON.stringify({ strange: { value:'9/14/2010 12:00:00 AM'} }),
4
  • When I changed the data to { strange: { value: '9/14/2010 12:00:00 AM' } }, null still comes in under value. I will look in to the output of stringify to see what happens there. Oct 24, 2010 at 15:37
  • This is what was posted: strange%5Bvalue%5D=9%2F14%2F2010+12%3A00%3A00+AM but it doesn't show up on the server under anything I can see (value remains null) Oct 24, 2010 at 15:38
  • @DrZim - Looks like you're not using JSON.stringify() like I have above ;) Oct 24, 2010 at 16:47
  • Adding Phil Haack's MVC2 Futures And using JSON.stringify() solved it. Apparently, there's a huge difference between my JSON and JSON.stringify()'s version. Thanks a lot for helping out. Oct 24, 2010 at 18:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.