HTML5技术

【ASP.NET Identity系列教程(二)】运用ASP.NET Identity - r01cn(13)

字号+ 作者:H5之家 来源:H5之家 2016-02-04 13:52 我要评论( )

The majority of the code in the GET version of the Edit action method is responsible for generating the sets of members and nonmembers of the selected role, which is done using LINQ. Once I have grou

The majority of the code in the GET version of the Edit action method is responsible for generating the sets of members and nonmembers of the selected role, which is done using LINQ. Once I have grouped the users, I call the View method, passing a new instance of the RoleEditModel class I defined in Listing 14-13.
GETEdit动作方法的主要代码是负责生成一组所选角色的成员和非成员,这是用LINQ完成的。一旦对用户进行了分组,便调用View方法,为其传递了清单14-13所定义的RoleEditModel类的新实例。

The POST version of the Edit method is responsible for adding and removing users to and from roles. The AppUserManager class inherits a number of role-related methods from its base class, which I have described in Table 14-9.
POST版的Edit方法是负责从角色中添加和删除用户。AppUserManager类从它的基类继承了几个与角色有关的方法,描述于表14-9。

Table 14-9. The Role-Related Methods Defined by the UserManager<T> Class
表14-9. UserManager<T>类中所定义的与角色有关的方法

Name
名称 Description
描述

AddToRoleAsync(id, name) Adds the user with the specified ID to the role with the specified name
将指定ID的用户添加到指定name的角色

GetRolesAsync(id) Returns a list of the names of the roles of which the user with the specified ID is a member
返回指定ID的用户所在的角色名列表

IsInRoleAsync(id, name) Returns true if the user with the specified ID is a member of the role with the specified name
如果指定ID的用户是指定name角色的成员,返回true

RemoveFromRoleAsync(id, name) Removes the user with the specified ID as a member from the role with the specified name
在指定name角色的成员中除去指定ID的用户

An oddity of these methods is that the role-related methods operate on user IDs and role names, even though roles also have unique identifiers. It is for this reason that my RoleModificationModel view model class has a RoleName property.
这些方法的奇怪之处在于,与角色有关的方法都根据“用户ID”和“角色name(角色名)”进行操作,尽管角色也具有唯一标识符(ID)。这也是在RoleModificationModel视图模型类中使用RoleName属性的原因。

Listing 14-15 shows the view for the Edit.cshtml file, which I added to the Views/RoleAdmin folder and used to define the markup that allows the user to edit role memberships.
清单14-15显示了Edit.cshtml文件的视图,该视图放在Views/RoleAdmin文件中,用它定义了让用户编辑角色成员的标记。

Listing 14-15. The Contents of the Edit.cshtml File in the Views/RoleAdmin Folder
清单14-15. Views/RoleAdmin文件夹中Edit.cshtml文件的内容

@using Users.Models @model RoleEditModel @{ ViewBag.Title = "Edit Role";} @Html.ValidationSummary() @using (Html.BeginForm()) { <input type="hidden" value="@Model.Role.Name" /> <div> <div>Add To @Model.Role.Name</div> <table> @if (Model.NonMembers.Count() == 0) { <tr><td colspan="2">All Users Are Members</td></tr> } else { <tr><td>User ID</td><td>Add To Role</td></tr> foreach (AppUser user in Model.NonMembers) { <tr> <td>@user.UserName</td> <td> <input type="checkbox" value="@user.Id"> </td> </tr> } } </table> </div> <div> <div>Remove from @Model.Role.Name</div> <table> @if (Model.Members.Count() == 0) { <tr><td colspan="2">No Users Are Members</td></tr> } else { <tr><td>User ID</td><td>Remove From Role</td></tr> foreach (AppUser user in Model.Members) { <tr> <td>@user.UserName</td> <td> <input type="checkbox" value="@user.Id"> </td> </tr> } } </table> </div> <button type="submit">Save</button> @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" }) }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    2017-05-02 11:02

  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • JS组件系列——自己动手封装bootstrap-treegrid组件 - 懒得安分

    JS组件系列——自己动手封装bootstrap-treegrid组件 - 懒得安分

    2017-04-28 14:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

网友点评
i