总是让空值在控制器阿贾克斯后控制器、让空值、阿贾克斯后

2023-09-10 14:34:12 作者:丗澗哪囿眞情在

我想从我的观点一个ajax后,如下图所示(使用jQuery)。

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

完整的解决方案这里

 $(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 });
        } 

型号此处, 但是,当我试图访问(断点检查)对象的属性( AddressInfo的),它总是表现出价值。我想没有字符串化和字符串化。我在学习MVC现在和初学者。请帮忙

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

推荐答案

这是不工作的原因是因为你使用ASP.NET MVC 2和模型的JSON绑定支持未添加直到ASP.NET MVC 3。

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.

您可以尽管添加功能ASP.NET MVC 2。菲尔哈克有一个帖子,描述的是,用一个链接来样code结尾:http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

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