views:

47

answers:

2

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.

+1  A: 

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'} }),
Nick Craver
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.
Dr. Zim
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)
Dr. Zim
@DrZim - Looks like you're not using `JSON.stringify()` like I have above ;)
Nick Craver
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.
Dr. Zim
+1  A: 

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.

Oleg
@Dr. Zim: Your problem is very easy and can be solved without the usage of MVC2 Futures. ASP.NET MVC is used mostly for the posting Form fields per Ajax. So if you have a function which return JSON you should use `dataType: "json"` in the `$.ajax` request, but you should send data in the default `"application/x-www-form-urlencoded"` `contentType`. So you should just remove `contentType: "application/json; charset=utf-8"` from your code. If you need a complex input data structures as input of the MVC controller you can declare input parameter like `string encodedInput` and decode it manually.
Oleg
@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
+1 for the suggestion. Very nice alternative. I will try that next.
Dr. Zim