How pass int array from jquery ajax to asp.net mvc action



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have tried many stackoverflow posts, but I have always the same result.
The passed int array always null.



Pass array to mvc Action via AJAX



Here the asp.net mvc action which accept the int array of ids.



[HttpPost]
public ActionResult Downloads(int ids)
return RedirectToAction("Exports", ids);



This is the ajax call, which is send the values.



var url = '@Url.Action("Downloads")';
var values = [1,2,3];
$.ajax(
url: url,
type: 'POST',
dataType: "json",
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify(ids: values )
);


I have no idea what I make wrong.
I have passed the ajax's date like a simple array: data: values or without stringify, but the asp.net mvc never accept the int array. It seems the traditional parameter of ajax object does not do anything.










share|improve this question
























  • @RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

    – Dabagab
    Nov 15 '18 at 13:42












  • Glad you got it working, I added it as an answer for you below

    – Rory McCrossan
    Nov 15 '18 at 13:54

















1















I have tried many stackoverflow posts, but I have always the same result.
The passed int array always null.



Pass array to mvc Action via AJAX



Here the asp.net mvc action which accept the int array of ids.



[HttpPost]
public ActionResult Downloads(int ids)
return RedirectToAction("Exports", ids);



This is the ajax call, which is send the values.



var url = '@Url.Action("Downloads")';
var values = [1,2,3];
$.ajax(
url: url,
type: 'POST',
dataType: "json",
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify(ids: values )
);


I have no idea what I make wrong.
I have passed the ajax's date like a simple array: data: values or without stringify, but the asp.net mvc never accept the int array. It seems the traditional parameter of ajax object does not do anything.










share|improve this question
























  • @RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

    – Dabagab
    Nov 15 '18 at 13:42












  • Glad you got it working, I added it as an answer for you below

    – Rory McCrossan
    Nov 15 '18 at 13:54













1












1








1








I have tried many stackoverflow posts, but I have always the same result.
The passed int array always null.



Pass array to mvc Action via AJAX



Here the asp.net mvc action which accept the int array of ids.



[HttpPost]
public ActionResult Downloads(int ids)
return RedirectToAction("Exports", ids);



This is the ajax call, which is send the values.



var url = '@Url.Action("Downloads")';
var values = [1,2,3];
$.ajax(
url: url,
type: 'POST',
dataType: "json",
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify(ids: values )
);


I have no idea what I make wrong.
I have passed the ajax's date like a simple array: data: values or without stringify, but the asp.net mvc never accept the int array. It seems the traditional parameter of ajax object does not do anything.










share|improve this question
















I have tried many stackoverflow posts, but I have always the same result.
The passed int array always null.



Pass array to mvc Action via AJAX



Here the asp.net mvc action which accept the int array of ids.



[HttpPost]
public ActionResult Downloads(int ids)
return RedirectToAction("Exports", ids);



This is the ajax call, which is send the values.



var url = '@Url.Action("Downloads")';
var values = [1,2,3];
$.ajax(
url: url,
type: 'POST',
dataType: "json",
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify(ids: values )
);


I have no idea what I make wrong.
I have passed the ajax's date like a simple array: data: values or without stringify, but the asp.net mvc never accept the int array. It seems the traditional parameter of ajax object does not do anything.







jquery asp.net-mvc asp.net-ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:23







Dabagab

















asked Nov 15 '18 at 13:17









DabagabDabagab

4822817




4822817












  • @RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

    – Dabagab
    Nov 15 '18 at 13:42












  • Glad you got it working, I added it as an answer for you below

    – Rory McCrossan
    Nov 15 '18 at 13:54

















  • @RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

    – Dabagab
    Nov 15 '18 at 13:42












  • Glad you got it working, I added it as an answer for you below

    – Rory McCrossan
    Nov 15 '18 at 13:54
















@RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

– Dabagab
Nov 15 '18 at 13:42






@RoryMcCrossan in this case I get a 'Invalid JSON primitive ids' error message. OK, content-type had to be removed too.

– Dabagab
Nov 15 '18 at 13:42














Glad you got it working, I added it as an answer for you below

– Rory McCrossan
Nov 15 '18 at 13:54





Glad you got it working, I added it as an answer for you below

– Rory McCrossan
Nov 15 '18 at 13:54












2 Answers
2






active

oldest

votes


















0














If you want to send values using traditional encoding then you shouldn't JSON encode the data you're sending. As such, remove the JSON.strigify() call, as well as the contentType. Try this:



$.ajax(
url: url,
type: 'POST',
dataType: "json",
traditional: true,
data: ids: values
);





share|improve this answer






























    0














    Just remove JSON.stringify it will work.



    [HttpPost]
    public JsonResult Test(int ids)

    return Json("Integers. " + ids);



    //In Jquery please put this.



    var PostData = ;
    PostData.ids = [1, 2, 3, 5];
    var ajaxOptions =
    type: "POST",
    url: '@Url.Action("Test", "Settings")',//Actionname, ControllerName
    data: PostData,
    dataType: "json",
    success: function (result)
    console.log(result);
    ,
    error: function (result)


    ;
    $.ajax(ajaxOptions);





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320381%2fhow-pass-int-array-from-jquery-ajax-to-asp-net-mvc-action%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you want to send values using traditional encoding then you shouldn't JSON encode the data you're sending. As such, remove the JSON.strigify() call, as well as the contentType. Try this:



      $.ajax(
      url: url,
      type: 'POST',
      dataType: "json",
      traditional: true,
      data: ids: values
      );





      share|improve this answer



























        0














        If you want to send values using traditional encoding then you shouldn't JSON encode the data you're sending. As such, remove the JSON.strigify() call, as well as the contentType. Try this:



        $.ajax(
        url: url,
        type: 'POST',
        dataType: "json",
        traditional: true,
        data: ids: values
        );





        share|improve this answer

























          0












          0








          0







          If you want to send values using traditional encoding then you shouldn't JSON encode the data you're sending. As such, remove the JSON.strigify() call, as well as the contentType. Try this:



          $.ajax(
          url: url,
          type: 'POST',
          dataType: "json",
          traditional: true,
          data: ids: values
          );





          share|improve this answer













          If you want to send values using traditional encoding then you shouldn't JSON encode the data you're sending. As such, remove the JSON.strigify() call, as well as the contentType. Try this:



          $.ajax(
          url: url,
          type: 'POST',
          dataType: "json",
          traditional: true,
          data: ids: values
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 13:54









          Rory McCrossanRory McCrossan

          251k29217254




          251k29217254























              0














              Just remove JSON.stringify it will work.



              [HttpPost]
              public JsonResult Test(int ids)

              return Json("Integers. " + ids);



              //In Jquery please put this.



              var PostData = ;
              PostData.ids = [1, 2, 3, 5];
              var ajaxOptions =
              type: "POST",
              url: '@Url.Action("Test", "Settings")',//Actionname, ControllerName
              data: PostData,
              dataType: "json",
              success: function (result)
              console.log(result);
              ,
              error: function (result)


              ;
              $.ajax(ajaxOptions);





              share|improve this answer





























                0














                Just remove JSON.stringify it will work.



                [HttpPost]
                public JsonResult Test(int ids)

                return Json("Integers. " + ids);



                //In Jquery please put this.



                var PostData = ;
                PostData.ids = [1, 2, 3, 5];
                var ajaxOptions =
                type: "POST",
                url: '@Url.Action("Test", "Settings")',//Actionname, ControllerName
                data: PostData,
                dataType: "json",
                success: function (result)
                console.log(result);
                ,
                error: function (result)


                ;
                $.ajax(ajaxOptions);





                share|improve this answer



























                  0












                  0








                  0







                  Just remove JSON.stringify it will work.



                  [HttpPost]
                  public JsonResult Test(int ids)

                  return Json("Integers. " + ids);



                  //In Jquery please put this.



                  var PostData = ;
                  PostData.ids = [1, 2, 3, 5];
                  var ajaxOptions =
                  type: "POST",
                  url: '@Url.Action("Test", "Settings")',//Actionname, ControllerName
                  data: PostData,
                  dataType: "json",
                  success: function (result)
                  console.log(result);
                  ,
                  error: function (result)


                  ;
                  $.ajax(ajaxOptions);





                  share|improve this answer















                  Just remove JSON.stringify it will work.



                  [HttpPost]
                  public JsonResult Test(int ids)

                  return Json("Integers. " + ids);



                  //In Jquery please put this.



                  var PostData = ;
                  PostData.ids = [1, 2, 3, 5];
                  var ajaxOptions =
                  type: "POST",
                  url: '@Url.Action("Test", "Settings")',//Actionname, ControllerName
                  data: PostData,
                  dataType: "json",
                  success: function (result)
                  console.log(result);
                  ,
                  error: function (result)


                  ;
                  $.ajax(ajaxOptions);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 13:37

























                  answered Nov 15 '18 at 13:28









                  Negi RoxNegi Rox

                  1,9351512




                  1,9351512



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320381%2fhow-pass-int-array-from-jquery-ajax-to-asp-net-mvc-action%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo