【发布时间】:2020-12-05 17:08:55
【问题描述】:
我有两个 json 数组。我正在尝试将这些 json 数组发送到我的控制器。当我想发送其中一个时,一切都很好,但我无法发送第二个。我该如何解决?
视图发布功能
function HobiIlgiAlanKaydetGuncelle() {
var hobiler = $('#Hobiler').val(); // Json array of object
var ilgiAlanlar = $('#IlgiAlan').val();
$.ajax({
url: "/Kullanici/HobiVeIlgiAlanlariniKaydetGuncelle",
type: "POST",
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
data: {hobiler : hobiler,ilgiAlanlar : ilgiAlanlar},
success: function (response) { }
});
}
控制器
[HttpPost]
public async Task<JsonResult> HobiVeIlgiAlanlariniKaydetGuncelle([FromBody] List<HobilerVM> hobiler, List<IlgiAlanlarVM> ilgiAlanlar)
{
//When I put second parameter, both of them comes with null
}
HobilerVM
public class HobilerVM
{
public int id { get; set; }
public string value { get; set; }
}
IlgiAlanVM
public class IlgiAlanVM
{
public int id { get; set; }
public string value { get; set; }
}
【问题讨论】:
-
这能回答你的问题吗? MVC3 & JSON.stringify() ModelBinding returns null model
JSON.stringify({hobiler : hobiler,ilgiAlanlar : ilgiAlanlar})应该这样做... -
我认为这对您的问题很有用:stackoverflow.com/questions/14407458/…
-
参数已经被 tagify 库字符串化。当我只发送 hobiler 时,我可以不使用 stringify 发送。
-
@Trinity:你使用的是哪个版本的 MVC?
标签: c# jquery .net ajax asp.net-mvc