[英]Parameters are coming null from ajax call
I have a following method in ASP.NET MVC:我在 ASP.NET MVC 中有以下方法:
public PartialViewResult SaveUpdateVariants(string TitleEn, string TitleAr, int ItemID, List<ItemVariant> data)
{
}
I am calling it from ajax call like this我像这样从 ajax 呼叫它
$.ajax({
url: '/Inventory/SaveUpdateVariants',
contentType: 'application/json; charset=utf-8', // Include ContentType
data : JSON.stringify(update),
method: "POST",
type: "POST",
success: result => {
gotoOptions();
$("#variant-table tbody").replaceWith(result);
window['variant-table'].refresh();
}
})
Update object is like this更新object是这样的
{
"TitleEn": "aa",
"TitleAr": "aa",
"ItemID": "829",
"data": [
{
"ItemVariantID": "-1",
"NameEn": "s",
"NameAr": "a",
"Amount": "10"
}
]
}
But my action method receiving all the parameter as null.但是我的操作方法接收所有参数为 null。
since you are using 'application/json;因为您使用的是“application/json; charset=utf-8' ContentType you have to add FromBody attribute to action and create in c# the same model as you have in java script
charset=utf-8' ContentType 您必须将 FromBody 属性添加到操作中,并在 c# 中创建与 java 脚本中相同的 model
public PartialViewResult SaveUpdateVariants([FromBody] Item item)
{
.........
}
public class Item
{
public string TitleEn {get; set;}
....
public List<ItemVariant> data {get;set;}
}
Can you try this你能试试这个吗
data : {TitleEn : update.TitleEn, TitleAr : update.TitleAr, ItemID : update.ItemID}
and so on..等等..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.