1

I tried an ajax post from my view as shown below (using jQuery).

Complete Solution Here.

 $(document).ready(function () {
                var kk = {
                    Address1: "423 Judy Road",
                    Address2: "1001",
                    City: "New York",
                    State: "NY",
                    ZipCode: "10301",
                    Country: "USA"
                };
                console.log(JSON.stringify(kk));
                $.ajax({
                    url: 'Check',
                    type: 'POST',
                    data: JSON.stringify(kk),
                    dataType:"json",
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        alert(data.success);
                    },
                    error: function () {
                        alert("error");
                    }
                });
            });

And received it in a controller (the method always invoked)

public ActionResult Check(AddressInfo addressInfo)
        {
            return Json(new { success = true });
        } 

Model here, But when I tried to access (break point checked) the properties of the object (AddressInfo) it always showed null value. I tried without stringifying and stringifying. I 'm learning MVC now and a beginner. Please help

Subin Jacob
  • 4,692
  • 10
  • 37
  • 69

4 Answers4

1

Try the following code:

return this.Json(new { success = true }, JsonRequestBehavior.AllowGet);

If this don't work just change the request parameter from AddressInfo to String at the controller side. This will definitely work!!

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Sanket
  • 286
  • 1
  • 3
  • 10
1

The reason this isn't working is because you're using ASP.NET MVC 2 and support for model binding from JSON wasn't added until ASP.NET MVC 3.

You can add that functionality to ASP.NET MVC 2 though. Phil Haack has a post describing that, with a link to sample code at the end: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • Can I use JSON.stringify(person) instead of $.toJSON(person). – Subin Jacob Oct 09 '13 at 03:26
  • Yes, they do the same thing in browsers that support JSON.stringify. In old browsers (pre-IE8, essentially), you need to include json2.js in order to use that. – Dave Ward Oct 09 '13 at 06:17
0

Add dataType as json in Ajax and pass addressInfo in data parameter like,

$.ajax({
    url: 'Check',
    type: 'POST',
    datatype:'json',
    data: {addressInfo:kk},
    success:function(data){
    ....
    ....
 });
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
-1

Try to pass data in querystring way like this:

    $(document).ready(function () {
        var data = "Address1=423 Judy Road&Address2=1001&City=New York&State=NY&ZipCode=10301&Country=USA";

        $.ajax({
            url: 'Check',
            type: 'POST',
            data: data,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    });
Flea777
  • 1,012
  • 9
  • 21