我想通过Asp.Net MVC中的Ajax提交页面输入。Ajax向控制器动作发送空参数
JQuery Ajax json数据不为null(在Console.log()中检查),但它将json字符串null传递给控制器操作。控制器动作方面的一个目的为字符串:
类别:
public int ID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public int BrandID { get; set; }
public int Price1 { get; set; }
public string Exchange { get; set; }
public bool State { get; set; }
控制器:
[HttpPost]
public ActionResult AddProduct(string data)
{
//string data comes null here
}
JQuery的:的console.log的
var xy ={
"data": {
CategoryID: categoryID,
BrandID: brandID,
ProductName: productName,
Price1: price1,
ExchangeName: exchangeName,
State: state
}
};
console.log(JSON.stringify(xy))
$.ajax({
url: "/Products/AddProduct/",
type: 'POST',
data: JSON.stringify(xy),
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function (xhr, status, error) {
alert(xhr.responseText)
}
});
输出(JSON.stringify(XY )):
{"data":{"CategoryID":"63","BrandID":"1","ProductName":"pname","Price1":"199","State":"1"}}
我检查了很多答案,但无法弄清楚我的问题。 感谢您的帮助。
console.log(JSON.stringify(xy));'?的输出是什么? – Sandman
“AddProduct”方法中有哪些参数? '数据'或'CategoryID'等? –
我更新了我的问题。 AddProduct方面的类参数作为字符串json数据来自ajax – Zeynep