【问题标题】:Razor View is passing wrong Id to OnPost methodRazor View 将错误的 Id 传递给 OnPost 方法
【发布时间】:2021-10-25 19:12:09
【问题描述】:

我正在使用带有 Razor 页面和身份的 Asp.net Core .Net 5 Web 应用程序。 我想为管理员构建一个角色管理器页面,但是在将角色分配给用户时,从视图传递的角色 ID 与我传递到视图的角色 ID 不匹配。 这是我的模型

public class RoleManagerModel : PageModel
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<IdentityRole> _roleManager;

    public RoleManagerModel(UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager)
    {
        _userManager = userManager;
        _roleManager = roleManager;
    }

    public IEnumerable<ApplicationUser> Users { get; set; }
    public IEnumerable<IdentityRole> Roles { get; set; }


    public ApplicationUser UserToChangeRole { get; set; }
    [BindProperty]
    public IdentityRole RoleToAssign { get; set; }
    public static string FirstID { get; set; }
    public static string SecondId { get; set; }

    public async Task OnGet()
    {
        Users = await _userManager.Users.ToListAsync();
        Roles = await _roleManager.Roles.ToListAsync();
        FirstID = Roles.FirstOrDefault(r => r.Name == "Admin").Id;

    }

    public async Task<IActionResult> OnPost(string userId)
    {
        UserToChangeRole = await _userManager.FindByIdAsync(userId);
        SecondId = RoleToAssign.Id;
        if(FirstID == SecondId)
        {
            string Gogo = "true";
        }
        var role = await _roleManager.FindByIdAsync(RoleToAssign.Id);
        if (UserToChangeRole == null)
            RedirectToPage("/Identity/NotFound");
        if (await _roleManager.RoleExistsAsync(RoleToAssign.Name))
        {
           await  _userManager.AddToRoleAsync(UserToChangeRole, RoleToAssign.Name);
        }

        Users = await _userManager.Users.ToListAsync();
        Roles = await _roleManager.Roles.ToListAsync();

        return Page();
    }

这是我认为的形式。

 <!--Roles-->
            <td>
                <form method="post">

                    <div class="form-group">
                        <label asp-for="Roles"></label>
                        <select class="form-control" asp-for="RoleToAssign">
                            @foreach (var role in Model.Roles)
                            {
                                <option value="@role.Id">@role.Name</option>
                            }
                        </select>

                    </div>

                    <button type="submit" class="btn btn-primary btn-sm">Assign</button>
                    <input type="hidden" name="userId" value="@user.Id" />
                </form>

            </td>

正如您在 Post 方法中看到的那样,我会检查传递和接收的 roleIds 是否匹配。他们没有。我究竟做错了什么?有什么建议?谢谢。

【问题讨论】:

  • 你试图从哪里获取 roleId 的代码?

标签: c# asp.net-core .net-core asp.net-identity razor-pages


【解决方案1】:

添加到页面模型

 [BindProperty]
 public int RoleToAssignId { get; set; }

并查看

<select class="form-control" asp-for="RoleToAssignId">
 @foreach (var role in Model.Roles)
 {
  <option value="@role.Id">@role.Name</option>
 }
</select>

【讨论】:

  • 谢谢,这确实有效!只有角色 ID 是字符串。 :),对不起,我不能为你的答案投票,需要提高我的声誉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多