从点击的用户传递值以查看模型

时间:2019-04-26 14:50:40

标签: c# asp.net-core

我正在创建一个基本的管理菜单。在“索引”页面中,列出了所有用户。当您单击按钮UpdateRole时,您将被重定向到UpdateRole页面,您可以在其中通过插入角色名称为用户赋予新角色。如何将我单击的用户的值传递给UpdateRoleViewModel,以便以后使用它?

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 个答案:

答案 0 :(得分:0)

您可以直接使用asp-route-{value},例如:

<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>

并使用viewModel访问值:

public async Task<IActionResult> UpdateRole(UpdateRoleViewModel model)