将文本框中的文本添加到Html.ActionLink

问题描述 投票:1回答:1

我有为用户添加角色的方法。当它常量字符串时,它工作正常。现在我想从输入中给出角色名称,这里我将文本传递给actionlink有问题。怎么做 ?这是我使用常量字符串的代码:

视图:

@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" })

控制器:

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");
        }
c# asp.net-mvc
1个回答
2
投票

使用表单发布值:

@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" />
}
© www.soinside.com 2019 - 2024. All rights reserved.