1

I have method that add roles for user. It works fine when it it constants string. Now i want to give role name from input and here i have problem with passing text to actionlink. How to do that ? Here is the code that i have working with constants string:

View:

@model AdminMVC.Models.Admin.UserViewModel
<input class="form-control" type="text" name="role" placeholder="Role name">
@Html.ActionLink("Add role", "AddRoleToUser", new { id = Model.ApplicationUser.Id }, new { @class = "fas fa-user-edit" })

Controller:

public async Task<IActionResult> AddRoleToUser(string id, string role)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (await _roleManager.RoleExistsAsync("NewRole"))
            {
                await _userManager.AddToRoleAsync(user, "NewRole");
            }

            return RedirectToAction("Roles");
        }
1
  • 1
    you have to use route values for your action link to send values to the controller. but then again you will have to get the input value and set it as the action link's route value. you can use a form to do that (replace the action link and use a submit button instead). or you could use jQuery to dynamically set the action link's route value before performing the request. Commented Feb 21, 2019 at 18:14

1 Answer 1

2

Use a form to post the value:

@using(Html.BeginForm("AddRoleToUser", new { id = Model.ApplicationUser.Id }))
{
  <input class="form-control" type="text" name="role" placeholder="Role name">
  <input type="submit" value="Add role" class="fas fa-user-edit" />
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.