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:
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));'の出力は何ですか? – Sandman
'AddProduct'メソッドにはどのようなパラメータがありますか? 'data'や' CategoryID'など? –
私の質問が更新されました。 AddProductアスペクトのクラスパラメータをajaxの文字列jsonデータとして返します – Zeynep