0

I am passing an array variable to the controller. Array from ajax call contains data but after calling the controller it shows count=0.

   var url = '@Url.Action("UserRoleCompany_AddUserAccess", "UserRoleCompany")';

        $.ajax({
            url: url,
            data: { userIDs: userIDs, Organisation: Organisation, RoleName: RoleName, userIDsLength: userIDsLength, UserStatus: UserStatus },
            cache: false,
            type: "POST",
            success: function (data) {
                location.reload(true);
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });

Controller code below,

  public ActionResult UserRoleCompany_AddUserAccess(List<int> userIDs, string Organisation, string RoleName, int userIDsLength,int UserStatus)
    {
        LMTUsage objLMT = new LMTUsage();
        LMTDAL objLMTDAL = new LMTDAL();

        objLMTDAL.UserRoleCompany_AddUserAccess(Organisation, RoleName, userIDsLength, UserStatus);

        return RedirectToAction("Index");
    }

And below are a screenshot for reference, enter image description here enter image description here

pankaj bawdane
  • 101
  • 4
  • 18

2 Answers2

0

You can not pass an array as a parameter in ajax,you can either convert userIDs to a json string or combine them as a string,then pass to the controller side.

More details information can be found at Why the array will not send through the ajax call?

flyingfox
  • 13,414
  • 3
  • 24
  • 39
0

@lucumt

I have tried the same thing with table - selecting the multiple rows from the table and send it to the controller and it is working fine.

Please check below and let me know.

    var url = '@Url.Action("UserRoleCompany_UpdateUserAccess", "UserRoleCompany")';

        $.ajax({
            url: url,
            data: { Ids: checkedIds, newUserStatus: UserStatus },
            cache: false,
            type: "POST",
            success: function (data) {
                location.reload(true);
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });

Controller

 public ActionResult UserRoleCompany_UpdateUserAccess(List<int> Ids, int newUserStatus)
    {
        LMTUsage objLMT = new LMTUsage();
        LMTDAL objLMTDAL = new LMTDAL();

        string userRoleIds = String.Join(",", Ids);

        objLMTDAL.UserRoleCompany_UpdateUserAccess(userRoleIds, newUserStatus);

        return RedirectToAction("Index");
        //return RedirectToAction("Index", "UserRoleCompany");
    }

You can check the live scenario below in screenshots, enter image description here enter image description here

pankaj bawdane
  • 101
  • 4
  • 18