【发布时间】:2020-06-19 15:10:47
【问题描述】:
尝试将对象发送到我的控制器:
$.ajax({
type: "POST",
url: '/Groups/Invite',
contentType: "application/json",
data: JSON.stringify(UserInvited),
dataType: "json",
success: function () {},
error: function (xhr, status, error) {}
});
在调试模式下,我的数据是:
{"Id":"47","Guest":[{"Key":"","Pseudo":"Lolo01500","Del":false,"Add":true}]}
我在控制器中的操作:
[HttpPost, ActionName("Invite")]
public IActionResult Invite(GroupInviteVM groupInviteVM)
{
// TODO
return Json(true);
}
和目标对象类:
public class ItemInvite
{
public string Key { get; set; }
public string Pseudo { get; set; }
public bool Add { get; set; }
public bool Del { get; set; }
}
public class GroupInviteVM
{
public int Id { get; set; }
List<ItemInvite> Guest { get; set; }
public GroupInviteVM()
{
Guest = new List<ItemInvite>();
}
}
执行时,只更新Id ...
有人可以帮助我吗?
T.Y.
【问题讨论】:
-
嗨@Ipupi,您是如何定义
UserInvited的?您对only the Id is updated是什么意思?您的Invite操作中似乎没有更新操作。 -
嗨丽娜。 UserInvited 是在我的 js 提交函数中构建的(在调用 ajax 之前)。它的值为 {"Id":"47","Guest":[{"Key":"","Pseudo":"Lolo01500","Del":false,"Add":true}]} 。但在我的操作中,我的 GroupInviteVM 对象中只初始化了 Id...Guest 始终为 null。
标签: javascript asp.net-core controller http-post