发送到C#控制器的敲除AJAX数据为空
我正在尝试将颜色列表(id号)发布到控制器中的测试函数。我知道数据正在发送,看起来格式正确 当我发送数据时,fiddler显示:发送到C#控制器的敲除AJAX数据为空,c#,asp.net-mvc-4,knockout.js,asp.net-ajax,C#,Asp.net Mvc 4,Knockout.js,Asp.net Ajax,我正在尝试将颜色列表(id号)发布到控制器中的测试函数。我知道数据正在发送,看起来格式正确 当我发送数据时,fiddler显示: [{"ID":"15","Duration":"Permanent","bNotPermanent":"1"}, {"ID":"21","Duration":"Permanent","bNotPermanent":"1"}] {"ID":"11","Duration":"Permanent","bNotPermanent":"false"} 击倒AJAX帖子
[{"ID":"15","Duration":"Permanent","bNotPermanent":"1"},
{"ID":"21","Duration":"Permanent","bNotPermanent":"1"}]
{"ID":"11","Duration":"Permanent","bNotPermanent":"false"}
击倒AJAX帖子:
SendData = ko.toJSON(self.AddColors) ;
alert(SendData);
$.ajax({
ContentType: 'application/json; charset=utf-8',
dataType: "json",
type: "POST",
url: '/EditColor/PostColors',
data: SendData
}).success(function (data) {
});
我正在使用的数据类:
public class AjaxColorList
{
public string ID { get; set; }
public string Duration { get; set; }
public string bNotPermanent { get; set; }
}
控制器功能:
(我还尝试了公共JsonResult PostColors(String AddColors只是为了看看是否有什么东西可以通过)
Fiddler展示了这一点:
[{"ID":"15","Duration":"Permanent","bNotPermanent":"1"},
{"ID":"21","Duration":"Permanent","bNotPermanent":"1"}]
{"ID":"11","Duration":"Permanent","bNotPermanent":"false"}
但是控制器将所有变量显示为null
这一定是我所缺少的愚蠢/简单的东西。有人能看到我逻辑中的错误吗?请尝试这种格式的ajax调用,我重新创建了场景,这对我很有用:
$.ajax({
url: '/Home/PostColors',
contentType: "application/json",
async: true,
type: "POST",
data: JSON.stringify(sendData),
error: function (jqXHR, textStatus, errorThrown) {
console.log("FAIL: " + errorThrown);
},
success: function (data, textStatus, jqXHR) {
console.log("SUCCES");
}
});
嗯。如果我这样做了,它希望我添加system.http.web;,它与system.mvc.web;冲突。如果我将system.mvc.web注释为使用system.http.web,我会列出各种编译器错误,因为控制器、viewresult等不再定义……这可能是针对WebAPI控制器的,这篇帖子建议添加[HttpPost]方法名称前的属性。本例将数组数据放入方法所需的参数名称中:这正是我在使用数组的第一次迭代中所做的,这就是为什么我不明白为什么数据在fiddler中清楚地显示为空。尝试上面更新的ajax格式,看看是否它不适合你。