Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass Id parameter into ajax function

AJAX function is not passing Id parameter to GET method in my controller.

I have this table.

 @foreach (var user in Model)
                {
                    <tr>
                        <td>@user.FirstName</td>
                        <td>@user.LastName</td>
                        <td>@user.Email</td>
                        <td>@string.Join(" , ", user.Roles.ToList())</td>
                        <td>
                            <a class="btn btn-primary" onclick="manageRolePopup('@Url.Action("Manage","Role",                                     new {id=user.UserId },Context.Request.Scheme)')">Manage Roles</a>

                            <partial name="_RoleManagePopup.cshtml" />
                        </td>
                    </tr>
                }

On click I want to show in popup user first name and last name so I have this in my Controller

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 [HttpGet]
        public async Task<IActionResult> Manage(string userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            ViewBag.FirstName = user.FirstName;
            ViewBag.LastName = user.LastName;

            var model = new ManageRoleViewModel();

            List<string> roleNames = new List<string>();

            foreach(var role in _roleManager.Roles)
            {
                model.RoleId = role.Id;
                roleNames.Add(role.Name);
            }
            model.UserId = user.Id;
            model.RoleNames = roleNames;

            return View(model);
        }

AJAX

manageRolePopup = (url) => {
    $.ajax({
        type: "GET",
        url: url,
        success: function (res) {
            $("#form-modal .modal-body").html(res);
            $("#form-modal").modal("show");
        }
    })
}

View

<form method="post" asp-controller="Role" asp-action="Manage" asp-route-UserId="@Model.UserId">
    <div class="row">
        <div class="col-3">
            <h4>@ViewBag.FirstName @ViewBag.LastName</h4>

            <div class="form-group">
                <select asp-items="@new SelectList(Model.RoleNames)">
                    <option selected disabled>---Select New Role---</option>
                </select>
            </div>
        </div>
    </div>
</form>

When im passing Id like below, everything is good. User is not null, Id parameter also.

<a asp-controller="Role" asp-action="Manage" asp-route-UserId="user.UserId"></a>

Obviously I want to do UPDATE method but for now I just want to have it displayed.

>Solution :

you have to add userId to your ajax url

var userId=$("#userId").val();
 $.ajax({
        type: "GET",
        url: url+"?UserId="+userId,
        ....

to get userId you have to add it as hidden field

  <td> <input type="hidden" value="@user.UserId" id="userId" /> </td>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading