admin管理员组

文章数量:1406052

I have an array like this:

var nums=["21", "22", "23", "20", "19", "18"];

and this JQuery Ajax codes:

 $('#chk').click(function () {

    $.ajax({
        url: "/BarberShop/ServiceUpdate",
        type: "Post",
        data:{ nums: nums},
        dataType: "json",
        success: function (data) {
        }
    });
});

and Controller Action like this:

    [HttpPost]
    public ActionResult ServiceUpdate(string nums)
    {
      //Do something......
        return View();
    }

the problem is when I Post the array using ajax numsparameter in Controller Action is null and also server gives me this error:

POST http://localhost:18322/BarberShop/ServiceUpdate 500 (Internal Server Error)

n.ajaxTransport.k.cors.a.crossDomain.send @ jquery-2.1.4.min.js:4n.extend.ajax @ jquery-2.1.4.min.js:4(anonymous function) @ Barbershop:481n.event.dispatch @ jquery-2.1.4.min.js:3n.event.add.r.handle @ jquery-2.1.4.min.js:3

I have tried all answer in this links and some others:

500-internal-server-error-on-jquery-ajax-post-asp-net-mvc

pass-array-to-ajax-request-in-ajax

but still have the problem.thanks

I have an array like this:

var nums=["21", "22", "23", "20", "19", "18"];

and this JQuery Ajax codes:

 $('#chk').click(function () {

    $.ajax({
        url: "/BarberShop/ServiceUpdate",
        type: "Post",
        data:{ nums: nums},
        dataType: "json",
        success: function (data) {
        }
    });
});

and Controller Action like this:

    [HttpPost]
    public ActionResult ServiceUpdate(string nums)
    {
      //Do something......
        return View();
    }

the problem is when I Post the array using ajax numsparameter in Controller Action is null and also server gives me this error:

POST http://localhost:18322/BarberShop/ServiceUpdate 500 (Internal Server Error)

n.ajaxTransport.k.cors.a.crossDomain.send @ jquery-2.1.4.min.js:4n.extend.ajax @ jquery-2.1.4.min.js:4(anonymous function) @ Barbershop:481n.event.dispatch @ jquery-2.1.4.min.js:3n.event.add.r.handle @ jquery-2.1.4.min.js:3

I have tried all answer in this links and some others:

500-internal-server-error-on-jquery-ajax-post-asp-net-mvc

pass-array-to-ajax-request-in-ajax

but still have the problem.thanks

Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked May 22, 2015 at 19:13 fbarikzehyfbarikzehy 5,4352 gold badges36 silver badges40 bronze badges 3
  • if you want it the other way where you pass a string instead of an array let me know. But, it should be simple where all you have to convert the array in javascript to a string. Anyways, let me know – AmmarCSE Commented May 22, 2015 at 19:22
  • also I have tried this but did not work:(string[] nums) – fbarikzehy Commented May 22, 2015 at 19:26
  • 1 I imagine string[] does not work because the array length is not know ahead of time. However, the size of List<string> is dynamic – AmmarCSE Commented May 22, 2015 at 19:28
Add a ment  | 

1 Answer 1

Reset to default 5

It is null because on the javascript side, you are using an array.

var nums=["21", "22", "23", "20", "19", "18"];

However, on the controller side, you are expecting a string.

public ActionResult ServiceUpdate(string nums)

Change the controller side to

public ActionResult ServiceUpdate(List<string> nums)

Also, in your ajax options, make sure you set traditional to true

$.ajax({
        url: "/BarberShop/ServiceUpdate",
        type: "Post",
        data:{ nums: nums},
        dataType: "json",
        success: function (data) {
        },
        traditional: true
    });

See How can I post an array of string to ASP.NET MVC Controller without a form?

Update

The reason you have to use List<string> has to do with parameter binding. When you ajax data as an array, the back-end parameter binder will look for a similar type/structure in the parameters to bind to. This is why you cannot bind a javascript Array to a C# string.

本文标签: javascriptPost an array using JQuery Ajax cause 500 (Internal Server Error) in MVCStack Overflow