我对 blazor 还很陌生,并且对向数据库添加角色感到有些疑问。
我已经实施了身份角色管理并拥有一个工作系统。
但现在我想通过 GUI 添加新角色,而不是编辑数据库。
我有一个名为 RolesOverview.razor 的 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 页面来完成此操作?
例子就完美了。
问候我!
Answer :
<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
});
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)