1

The javascript side of things looks like this:

var self = this;
    self.services = ko.observableArray([]);

    self.saveServices = function () {
        if (self.services().length > 0) {
            
            var obj = JSON.stringify({ services: self.services() });

            $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: "POST",
                url: '/Business/SaveServices',
                data: obj,
                success: function (data) {
                    $("#saveModal").modal('show');
                    if (!data) {
                        $("#saveDetails").text('Processing error, please try again or email us.');
                        return;
                    } else if (data === "saved") {
                        $("#saveDetails").text('Saved changes.');
                        setTimeout(function () {
                            $('#saveModal').modal('hide');
                        }, 2500);
                    } else {
                        $("#saveDetails").text(data);
                    }
                },
                error: function (error) {
                    $("#saveModal").modal('show');
                    $("#saveDetails").text('Processing error, please try again or email us.');
                }
            });
        }
    }

Then my controller looks like this:

[HttpPost]
    public async Task<JsonResult> SaveServices([FromBody]SaveServicesRequest saveServicesRequest)
    {
        try
        {
            if (User.Identity.IsAuthenticated)
            {
                return Json("saved");
            }
            return Json("User is not authenticated.");
        }
        catch (Exception ex)
        {
            logger.Error(ex + ". Users Email address: " + User.Identity.Name);
            return Json("Processing error, please try again or email us.");
        }
    }
    public class SaveServicesRequest
    {
        public List<services> services { get; } = new List<services>();
    }
    public class services
    {
        public string service { get; set; }
    }

Am hitting the controller but the array of items I'm expecting is not returned (saveServicesRequest is empty), I cannot seem to pass an array in any form. Anyone see what am doing wrong? The data/json is being sent client side, think the problem might lie in how am building the json and receiving it.

3
  • Did you observe what data is passed from client to server using some network tracing tools? You can use network tab of developer tools of your browser to see what data is sent. Commented Jan 7, 2022 at 1:20
  • Can you pls post self.services() data too? Commented Jan 7, 2022 at 1:52
  • Could you pls share more details about your view? Commented Jan 7, 2022 at 2:46

1 Answer 1

1

The problem is that you're initializing services to a new list in SaveServicesRequest. The properties should also have public getters and setters for model binding.

Try updating your class to:

public class SaveServicesRequest
{
    public List<services> services { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It was the class that was the issue exactly as you mentioned, thank you :) the knockout client side aspect didn't like the that change above but am retrieving the data now so all good, thanks again
@MartinCooke Awesome! I was sure about the class change but I wasn't sure about the knockout change and added it to my answer later - have rolled back to original answer now. Glad you got it working!

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.