I am creating a basic admin menu. Within the Index page, I have all users listed. When you click on the button UpdateRole, you will be redirected to the UpdateRole page where you can give a user a new role by inserting the role name. How can I pass the value of the user I've clicked on to the UpdateRoleViewModel, so I can use it afterwards?

UpdateRole.cshtml


@{
    ViewData["Title"] = "UpdateRole";
}

<h1 class="headingMarginBottom display-4">Update user role</h1>

<div class="form-row">
    <form class="col-lg-7" method="post">
        <div class="form-group">
            <label asp-for="RoleName">RoleName</label>
            <input class="form-control" asp-for="RoleName" />
            <span asp-validation-for="RoleName"></span>
        </div>

        <div class="form-group">
            <button class="btn btn-success" type="submit">Create!</button>
        </div>
    </form>
</div>

UpdateRoleViewModel.cs

    {
        [Required]
        public string UserName { get; set; }

        [Required]
        public string RoleName { get; set; }
    }

Index.cshtml


@{
    ViewData["Title"] = "Admin Dashboard";

}
<div class="container">
    <div class="row">
        <div class="col-md-11 text-md-center">
            <h1 class="headingMarginBottom display-4">Administration Dashboard</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h1 class="display-4">Players</h1>
            <div>
                @foreach (var user in Model.Users)
                {
                    <partial name="_UserPartial" model="user" />
                    <hr />
                }
            </div>
        </div>
    </div>
</div>

_UserPartial.cshtml

@model ApplicationUserViewModel
@{
    ViewData["Title"] = "_UserPartial";
}

<h4 class="teamDescription">Username: @Model.Username Email: @Model.Username </h4>

<a asp-area="Administration" asp-controller="Admin" asp-action="UpdateRole" asp-route-id="@Model.Username" class="btn btn-default">UpdateRole</a>

1 Answers

0
Xing Zou On

You could use asp-route-{value} directly like:

<a asp-area="Administration" asp-controller="Admin" asp-action="UpdateRole" asp-route-username="@Model.Username" asp-route-rolename="@Model.RoleName" class="btn btn-default">UpdateRole</a>

And access the value using viewModel:

public async Task<IActionResult> UpdateRole(UpdateRoleViewModel model)