Post JavaScript array with AJAX to asp.net MVC controller

前端 未结 6 2197
长情又很酷
长情又很酷 2020-12-01 12:23

My controller:

[HttpPost]
public ActionResult AddUsers(int projectId, int[] useraccountIds)
{
    ...
}

I\'d like to post the parameters to

6条回答
  •  旧时难觅i
    2020-12-01 12:58

    In JS:

    var myArray = new Array();
    myArray.push(2);
    myArray.push(3);
    $.ajax({
                type: "POST",
                url: '/MyController/MyAction',
                data: { 'myArray': myArray.join() },
                success: refreshPage
            });
    

    In MVC/ C#:

    public PartialViewResult MyAction(string myArray)
    {
       var myArrayInt = myArray.Split(',').Select(x=>Int32.Parse(x)).ToArray();
       //My Action Code Here
    }
    

提交回复
热议问题