【发布时间】:2021-03-01 17:32:07
【问题描述】:
我尝试了很多方法来做到这一点,但每次 ajax 都将 null 发送到控制器。我有 json 对象数组,如下所示。我想将此数组发送到我的控制器,但每次控制器参数为空。我做错了什么?
{
"barkodliste":[
{
"ToplamSonuc":0,
"KitapKey":12,
"DemirbasNo":112,
"Isbn":"fghfgh",
"KutuphaneSiraNo":"fghfghh/fghfg",
"KitapAdi":"0000000000112 - Fghfg",
"Adet":0,
"Durum":1
},
{
"ToplamSonuc":0,
"KitapKey":15,
"DemirbasNo":115,
"Isbn":"sdlfkjsd",
"KutuphaneSiraNo":"fsdkljflskdj/dsfkl",
"KitapAdi":"0000000000115 - Sdlfkjsd",
"Adet":0,
"Durum":1
}
]
}
KitapAramaSonucModel.cs
public class KitapAramaSonucModel
{
public int ToplamSonuc { get; set; }
public int KitapKey { get; set; }
public int DemirbasNo { get; set; }
public string Isbn { get; set; }
public string KutuphaneSiraNo { get; set; }
public string KitapAdi { get; set; }
public string Yazar { get; set; }
public int Adet{ get; set; }
public int Durum { get; set; }
public string Barkod { get; set; }
}
控制器
[HttpPost]
public async Task<JsonResult> BarkodBasilacakKitaplariGetir(List<KitapAramaSonucModel> barkodlar)
{
//Do something with barkodlar
}
cshtml
var barkodliste = $("#books").data("kendoMultiSelect").dataItems();
$.ajax({
cache: false,
url: "/Kutuphane/BarkodBasilacakKitaplariGetir",
type: 'POST',
contentType: "application/json charset=utf-8",
traditional: true,
data: JSON.stringify({ barkodlar: barkodliste }),
success: function (data) {
alert("success");
}
});
【问题讨论】:
-
您需要创建一个具有
List<KitapAramaSonucModel> barkodliste { get; set;}属性的类,并将该类用作动作方法中的模型。
标签: c# .net ajax model-view-controller