[英]passing an array from ajax call to controller, array empty in controller
Can anyone suggest what I need to change here?谁能建议我在这里需要改变什么?
I'm getting classes from elements that contain the class 'changed', the classes I get contain id's that I want to pass to the controller method.我从包含 class '已更改'的元素中获取类,我得到的类包含我想传递给 controller 方法的 id。
When I look in the network tab I can see the data I want in the payload, it looks like this:当我查看网络选项卡时,我可以在有效负载中看到我想要的数据,它看起来像这样:
{"list":["GroupId-1","SubGroupId-2","changed"]}
but when I put a breakpoint on the controller the list is null.但是当我在 controller 上设置断点时,列表是 null。
This is the class I'm expecting in the controller method:这是我在 controller 方法中期望的 class :
public class MemberGroups
{
public string GroupId { get; set; }
public string SubGrouId { get; set; }
public string Status { get; set; }
}
javascript for the save javascript 用于保存
function Savechanges() {
var spans = document.getElementsByClassName("changed");
var list = [];
$.each(spans,
function (key, value) {
$.each(value.classList,
function (key, value) {
list.push(value);
});
});
var dataToPost = JSON.stringify({ list: list });
$.ajax({
url: "/umbraco/Api/OrganisationMemberGroupsDashboardApi/UpdateMemberToGroup",
data: JSON.stringify({ list }),
type: "POST",
contentType: "application/json; charset=utf-8", // specify the content type
})
.done(function (data) {
});
}
controller controller
public string UpdateMemberToGroup( List<MemberGroups> list)
{
// save records
}
The spans are created dynamically and added to a treeview.跨度是动态创建的,并添加到 treeview。 When they are dragged and dropped all classes are removed then the 'changed' class is added along with the id classes so I only pass the ones I need to to the controller
当它们被拖放时,所有类都被删除,然后“更改” class 与 id 类一起添加,所以我只将我需要的类传递给 controller
var s = document.createElement('span');
s.classList.add('node-facility');
s.classList.add('ui-droppable');
s.classList.add('GroupId-' + value.GroupId);
s.classList.add('SubGroupId-0');
s.id=('GroupId-' + value.GroupId);
s.appendChild(document.createTextNode(value.GroupName));
This variant was tested using postman body json - ["GroupId-1","SubGroupId-2","changed"]此变体已使用 postman 主体 json - ["GroupId-1","SubGroupId-2","changed"] 进行了测试
Change your ajax data to this:将您的 ajax 数据更改为:
data: list,
and your controller action:和你的 controller 动作:
public string UpdateMemberToGroup([FromBody] []string list)
{
var memberGroups = new MemberGroups
{
GroupId =list[0],
SubGrouId =list[1],
Status =list[2]
};
// save records
}
This variant was tested in postman using {"GroupId":"GroupId-1","SubGroupId": "SubGroupId-2", "Status":"changed"}此变体在 postman 中使用 {"GroupId":"GroupId-1","SubGroupId": "SubGroupId-2", "Status":"changed"} 进行了测试
you can put the code in javascript:您可以将代码放在 javascript 中:
var data={GroupId:list[0],SubGroupId:list[1], Status:list[2]}
......
....
data:data,
.....
your controler action in this case:在这种情况下,您的控制器操作:
public string UpdateMemberToGroup([FromBody] MemberGroups memberGroups)
{
// save records
}
And I don't know what version MVC you use, but for some versions instead of [FromBody] better to use [FromForm] or don't use anything at all.而且我不知道您使用什么版本的 MVC,但是对于某些版本而不是 [FromBody] 更好地使用 [FromForm] 或根本不使用任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.