使用UI界面创建角色

时间:2018-11-08 20:29:34

标签: c# asp.net-core .net-core

我是使用asp.net core 2的新手,我试图添加一个视图以在网页中创建角色,所以我的方法类似于:

 public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
        {
            try
            {
                foreach (var role in model.RoleList)
                {
                    var roleExists = await _roleManager.RoleExistsAsync(role.Name);
                    if (roleExists) continue;
                    var createRole = _roleManager.CreateAsync(role);
                }
                foreach (var item in model.RoleClaimList)
                {
                    var currentClaimList = await _roleManager.GetClaimsAsync(item.Role);
                    var needToAddClaims = currentClaimList.Except(item.ClaimList);
                    var needToRemoveClaims = item.ClaimList.Except(currentClaimList);
                    foreach (var claim in needToAddClaims)
                    {
                        await _roleManager.AddClaimAsync(item.Role, claim);
                    }
                    foreach (var claim in needToRemoveClaims)
                    {
                        await _roleManager.RemoveClaimAsync(item.Role, claim);
                    }
                }
                return Ok();
            }
            catch (Exception)
            {
                return BadRequest();
            }
        }

ApplicationRoleModel模型

public class ApplicationRoleModel : ClaimsToRoleModel
    {
        public IEnumerable<ApplicationRole> RoleList { get; set; }
    }

ClaimsToRoleModel模型

public class ClaimsToRoleModel
    {
        public IEnumerable<RoleClaimModel> RoleClaimList { get; set; }
    }

    public class RoleClaimModel
    {
        public ApplicationRole Role { get; set; }
        public IEnumerable<Claim> ClaimList { get; set; }
    }

所以我创建了一个像这样的视图:

@page
@model Security.Dto.Models.ApplicationRoleModel
@{
    ViewData["Title"] = "Create New Role";
}

<h2>@ViewData["Title"]</h2>

<div class="row">
    <div class="col-md-4">
        <form asp-controller="security" asp-action="createrole" method="post">
            <h4>Create new Role.</h4>
            <hr />
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name">Name</label>
                <input asp-for="Name" class="form-control" />

            </div>
            <button type="submit" class="btn btn-default">Create</button>
        </form>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

但是当我尝试构建项目时,它会返回

  

'ApplicationRoleModel'不包含'Name'的定义,并且没有   扩展方法“名称”接受类型的第一个参数   可以找到“ ApplicationRoleModel”(您是否缺少使用   指令还是程序集引用?)

应该如何回应我的模型?我真的在那里感到困惑。问候

1 个答案:

答案 0 :(得分:1)

在您看来,您引用了不存在的form属性,因此出现错误。

在视图的第一行中,您已声明类型为ApplicationRoleModel的模型,并且在表单中已声明了该模型上不存在的'Name'属性的输入。

要解决该问题,您可以向Name添加一个ApplicationRoleModel属性-尽管我会更清楚地使用NewRoleName

public class ApplicationRoleModel : ClaimsToRoleModel
{
    [BindNever] // BindNever is to avoid model-binding security issues
    public IEnumerable<ApplicationRole> RoleList { get; set; }

    [Required]
    [Display(Name="Name" )] // This will be displayed in the `<label asp-for>` element
    public String NewRoleName { get; }
}

在您的cshtml中:

<div class="form-group">
    <label asp-for="Name" />
    <input asp-for="Name" class="form-control" />
 </div>

如果要允许编辑多个项目,请将视图模型更改为使用List<T>而不是IEnumerable<T>并使用for( Int32 i = 0; i < list.Count; i++ )循环(不能使用{{1} }),就像这样:

foreach

在您的cshtml中:

public class NewRoleViewModel
{
    //[Required] // no-longer required so that users can submit empty lists
    [Display(Name="Name" )]
    public String NewRoleName { get; set; }
}

public class ApplicationRoleModel : ClaimsToRoleModel
{
    [BindNever] // BindNever is to avoid model-binding security issues
    public IEnumerable<ApplicationRole> RoleList { get; set; }

    [Required]
    public List<NewRoleViewModel> NewRoles { get; } = new List<NewRoleViewModel>();
}

请注意,@for( Int32 i = 0; i < this.Model.NewRoles.Count; i++ ) { <div class="form-group"> <label asp-for="NewRoles[i].Name" /> <input asp-for="NewRoles[i].Name" class="form-control" /> </div> } 列表默认情况下为空,您需要在Controller操作方法中添加一些空条目:

NewRoles