gpt4 book ai didi

jquery - 如何将此 js 数组传递到我的 MVC 3 Controller ?

转载 作者:行者123 更新时间:2023-12-03 22:25:26 25 4
gpt4 key购买 nike

我在 Controller 中得到空值。不确定我错过了什么。

我有一个网格,其中有一个客人列表(带有姓名和电子邮件),用户可以通过复选框选择客人。

然后我读取所选联系人的姓名和电子邮件并构建 js 数组。然后这个数组被传递到 MVC 3 Controller

JS代码:

var name ='', email='';
var guest = new Array();
var guests = new Array();
$('.CBC').each(function () { //loop grid by checkbox class
if (this.checked) {
name = GetSelectedName();
email = GetSelectedEmail();
guest = { 'Email': email, 'Name': name };
guests.push(guest);
}
});

$.ajax({
type: "POST",
url: GetURL(),
data: guests,
dataType: "json",
success: function (res) {
//do something
}
});

Controller :

[HttpPost]
public ActionResult AddGuests(List<SelectedGuest> guests)
{
GuestService svc = new GuestService();
//do something with guests
//But Name and Email of all items in guests are null!!!
}

public class SelectedGuest
{
//represent the email columns of the contact grid
public string Email { get; set; }

//represent the Name column of the contact grid
public string Name { get; set; }
}

我需要显式地将js数组转换为json对象来序列化它吗?

最佳答案

$.ajax({
type: "POST",
url: "yourUrl",
data: JSON.stringify(yourArray),
contentType: "application/json; charset=utf-8"
});

contentType: "application/json; charset=utf-8"- 非常重要的部分

[HttpPost]
public void Fake(List<yourType> yourArray)
{
}

关于jquery - 如何将此 js 数组传递到我的 MVC 3 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com