JQuery的Ajax和ASP.NET MVC3导致空参数 [英] JQuery Ajax and ASP.NET MVC3 causing null parameters
问题描述
这个问题已经被问过很多次,但我没有找到解决方案,似乎工作。让我觉得这可能是一个新的问题,也许一些具体的事情到ASP.NET MVC 3。
This question has been asked many times but none of the solutions I found seem to be working. Makes me think this could be a new issue, and maybe something specific to ASP.NET MVC 3.
我使用JQuery Ajax来作出一个ASP.NET MVC 3控制器的简单调用。像这样
I am using JQuery Ajax to make a simple call to an ASP.NET MVC 3 controller. Like so
var addressInfo = {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA"
};
$.ajax({
url: '/home/check',
type: 'POST',
data: JSON.stringify(addressInfo),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
控制器看起来像这样
The controller looks like this
[HttpPost]
public ActionResult Check(AddressInfo addressInfo)
{
// Do something and return Json(something)
}
这是不行的,但它应该基于Scottgu的的JavaScript和AJAX技术的改进一节<一href="http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx">post
This does not work, though it should have based on the "JavaScript and AJAX Improvements" section of Scottgu's post
我已经试过这许多不同的变化,例如:
I have tried many different variations of this, for example:
var args = new Object();
args.addressInfo = {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA"
};
$.ajax({
url: '/home/check',
type: 'POST',
data: JSON.stringify(args),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
然后两个以上直接与JSON对象的:
And then both of the above directly with the JSON object:
$.ajax({
url: '/home/check',
type: 'POST',
data: args,
success: function () {
alert("success");
},
error: function () {
alert("error");
}
});
无工作。如果我只有我传递给控制器的字符串,该工程。但只要我介绍对象,我不能让它的工作。
None work. If I only have a string that I pass to the controller, that works. But as soon as I introduce an object, I cannot get it to work.
任何人都知道可能是什么问题。
Anyone know what might be the issue.
非常感谢寻找到这!
推荐答案
您code似乎不错,它应该工作。我刚刚测试了以下在一个新的应用程序没有问题。
Your code seems fine and it should work. I've just tested the following in a new application without problems.
型号:
public class AddressInfo
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Check(AddressInfo addressInfo)
{
return Json(new { success = true });
}
}
查看:
<script type="text/javascript">
var ai = {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA"
};
$.ajax({
url: '/home/check',
type: 'POST',
data: JSON.stringify(ai),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data.success);
},
error: function () {
alert("error");
}
});
</script>
这篇关于JQuery的Ajax和ASP.NET MVC3导致空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!