0

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.

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.

This is the class I'm expecting in the controller method:

public class MemberGroups
{
    public string GroupId { get; set; }
    public string SubGrouId { get; set; }
    public string Status { get; set; }
}

javascript for the save

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

public string UpdateMemberToGroup( List<MemberGroups> list)
{
    // save records
}

The spans are created dynamically and added to a 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

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));
3
  • can you show your spans data, pls? Commented Jan 24, 2021 at 14:06
  • just updated the post, thanks Commented Jan 24, 2021 at 14:14
  • Thanks, but I would like to seee your spans var . Commented Jan 24, 2021 at 14:31

1 Answer 1

1

This variant was tested using postman body json - ["GroupId-1","SubGroupId-2","changed"]

Change your ajax data to this:

data:  list,

and your controller action:


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"}

you can put the code in 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.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the suggestions, both still passing null to the controller though
It works in my visual studio test. Can you check this - alert(JSON.Strigify(list)), pls?
the alert gives ["changed","SubGroupId-2","GroupId-1"] which is what I'd expect. this is how my ajax call looks: var memberGroups = { GroupId: list[0], SubGroupId: list[1], Status: list[2] } alert(JSON.stringify(list)); $.ajax({ url: "/umbraco/Api/OrganisationMemberGroupsDashboardApi/UpdateMemberToGroup", data: memberGroups, type: "POST", contentType: "application/json; charset=utf-8" })
Try to remove ContentType. I usually use it only for the special types. And Instead of done I usually use - success: function (result) { ...} . And did you try [FromForm] instead of [FromBody] or remove it at all?
removing the type did it, thanks for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.