【发布时间】:2017-10-31 02:00:40
【问题描述】:
我想在 Asp.Net MVC 中通过 Ajax 提交页面输入。
JQuery Ajax json 数据不为空(在 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:
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)
}
});
console.log(JSON.stringify(xy)) 的输出:
{"data":{"CategoryID":"63","BrandID":"1","ProductName":"pname","Price1":"199","State":"1"}}
我检查了很多答案,但无法弄清楚我的问题。 感谢您的帮助。
【问题讨论】:
-
console.log(JSON.stringify(xy));的输出是什么? -
AddProduct方法中有哪些参数?data或CategoryID等? -
我更新了我的问题。将产品方面类参数添加为来自 ajax 的字符串 json 数据
标签: jquery json ajax asp.net-mvc