【问题标题】:Blazor Role Management Add Role trough UI (Crud)Blazor 角色管理通过 UI (Crud) 添加角色
【发布时间】:2020-11-20 03:27:07
【问题描述】:

我对 blazor 还很陌生,并且对向数据库添加角色有些疑问。 我已经实施了身份角色管理并拥有一个工作系统。 但现在我想通过 GUI 添加新角色,而不是编辑数据库。

我有一个名为 RolesOverview.razor 的剃须刀页面 在此页面上,我有一个输入字段和一个按钮。 当我单击此按钮时,我想将文本添加到角色管理器并将其保存到数据库中。

这是我的剃须刀组件

@page "/admin/roles"
@using Microsoft.AspNetCore.Identity
@inject RoleManager<IdentityRole> roleManager

<div class="jumbotron">
    <!-- Roles Overview Group Box -->
    <div class="row mb-5">
        <div class="col-12">
            <h1 class="display-6">Roles Options</h1>
            <hr class="my-4" />
            <div class="row" style="background-color:white; margin-bottom:10px; margin-top:10px;">
                <div class="col-12">
                    <div class="card w-100 mb-3" style="min-width:100%;">
                        <div class="card-body">
                            <h5 class="card-title">Roles</h5>
                            <p class="card-text">
                                <div class="row">
                                    <div class="col-1">
                                        Role Name:
                                    </div>
                                    <div class="col-10">
                                        <input type="text" style="min-width:100%;" placeholder="Role Type" />
                                    </div>
                                    <div class="col-1">
                                        <a href="#!" class="btn btn-primary" style="min-width:90px;">Add Role</a>
                                    </div>
                                </div>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

未获救...

  @code {
        private string CurrentValue { get; set; }

        private async void AddRole()
        {
            if (CurrentValue != string.Empty)
            {
                if (!await roleManager.RoleExistsAsync(CurrentValue))
                {
                    await roleManager.CreateAsync(new IdentityRole
                    {
                        Name = CurrentValue
                    });
                }
            }
        }

    }

我不知道接下来要做什么。 我是否可以使用 razor 组件进行操作,还是需要通过 razor 页面进行操作?

示例将是完美的。

问候我!

【问题讨论】:

    标签: c# .net visual-studio crud blazor


    【解决方案1】:

    答案:

                                        <div class="col-10">
                                            <input value="@CurrentValue" @onchange="@((ChangeEventArgs __e) => CurrentValue =__e.Value.ToString())" />
    
                                            @*<input type="text" style="min-width:100%;" placeholder="Role Type" />*@
                                        </div>
                                        <div class="col-1">
                                            <a @onclick="AddRole" class="btn btn-primary" style="min-width:90px;">Add Role</a>
                                        </div>
    

    @代码{ 私有字符串 CurrentValue { 获取;放; }

            private async void AddRole()
            {
                if (CurrentValue != string.Empty)
                {
                    if (!await roleManager.RoleExistsAsync(CurrentValue))
                    {
                        await roleManager.CreateAsync(new IdentityRole
                        {
                            Name = CurrentValue
                        });
                    }
                }
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用 RoleManager 通过 CreateAsync 方法创建新角色:

      if (!await roleMgr.RoleExistsAsync("RoleName"))
      {
          await roleManager.CreateAsync(new IdentityRole
          {
              Name = "RoleName"
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-05
        • 2023-03-06
        • 2021-04-18
        • 2020-09-26
        • 2020-12-26
        • 2011-02-17
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多