0

I'm attempting to pass an id (long) plus a list of ids (array) to a controller action. The best luck I've had is getting the id to pass, but I've never gotten the array to post.

I've gone through at least 10 stack overflow articles today trying to get this to work. I'm sure I'm missing something minor, but I'm not sure what.

Javascript & AJAX:

        HandleCardReordering: function () {
            var ids = new Array();
            for (var i = 0; i < $(".CourseSectionId").length; i++) {
                ids[i] = $(".CourseSectionId:eq(" + i + ")").val();
            }
            var courseId = $("#CourseId").val();
            
            $.ajax({
                url: "/Content/CourseContent/ReorderCourseSections?courseId=" + courseId + "&sectionIds=" + JSON.stringify(ids),
                cache: false,
                type: "POST",
                dataType: 'json',
                contentType: "application/json",
                traditional: true,
                success: function (result) {
                    //
                }
            });
        },

The ids collection and courseId are valid, I checked them with console.log and they look good.

My Controller method:

        [HttpPost]
        public async Task<JsonResult> ReorderCourseSections(long courseId, [FromQuery]long[] sectionIds, CancellationToken cancellationToken)
        {
            return Json(new { success = "true" });
        }

I can get courseId to populate via the query string parameter, but sectionIds is always a length 0 array. I tried adding traditional: true, tried to stringify my ids collection, I added [FromQuery] to my sectionIds parameter. None worked. Thanks for looking.

3
  • 1
    Your parameter names and types need to match. You're passing sectionIds as a string - MVC doesn't know you want that automatically parsed and the default model binder doesn't know how to convert it for you. You have options 1) add a model binder that converts the json to a int array 2) change your parameter to a string (do this in the first instance) and parse it yourself in the action 3) change the format of how you pass your sectionIds (ie not as a JSON string) Commented Oct 22, 2023 at 17:01
  • I ended up using your #2 suggestion, and that works. If you can post your comment in the form of an answer I will accept it. Thanks. Commented Oct 22, 2023 at 23:28
  • That's just the first-pass to get it working. See this article. And this SO question on how to add a custom model binder. Commented Oct 23, 2023 at 8:09

1 Answer 1

0

Possible solution:

1. First Approach:

1- Change Parameter type from long[] to string. 2- You will get a string now parse it and convert it into long[] in controller.

public ActionResult Multiple(string sectionIds){
  long[] idsArray = sectionIds.Split(',').Select(long.Parse).ToArray();
  
}

Second Approach:

1- Change your queryparamter something like below example

?sectionIds=24041&sectionIds=24117

No need to change in controller . In .Netcore it will work.

http://localhost:5434/MukeshNProject/MultipleIds?ids=24041&ids=24117
public ActionResult MultipleIds([FromQuery] int[] ids)
{
}
Sign up to request clarification or add additional context in comments.

Comments

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.