Custom HTML helper methods are defined as extensions on the HtmlHelper class. My helper, which is called GetUsername, takes a string argument containing a user ID, obtains an instance of the AppUserManager through the GetOwinContext.GetUserManager method (where GetOwinContext is an extension method on the HttpContext class), and uses the FindByIdAsync method to locate the AppUser instance associated with the ID and to return the value of the UserName property.
Õâ¸ö×Ô¶¨ÒåµÄHTML¸¨ÖúÆ÷·½·¨£¬ÊÇ×÷ΪHtmlHelperÀàµÄÀ©Õ¹½øÐж¨ÒåµÄ¡£¸Ã¸¨ÖúÆ÷µÄÃû³ÆΪGetUsername£¬ÒÔÒ»¸öº¬ÓÐÓû§IDµÄ×Ö·û´®Îª²ÎÊý£¬Í¨¹ýGetOwinContext.GetUserManager·½·¨»ñÈ¡AppUserManagerµÄÒ»¸öʵÀý£¨ÆäÖÐGetOwinContextÊÇHttpContextÀàµÄÀ©Õ¹·½·¨£©£¬²¢Ê¹ÓÃFindByIdAsync·½·¨¶¨Î»ÓëIDÏà¹ØÁªµÄAppUserʵÀý£¬È»ºó·µ»ØUserNameÊôÐÔµÄÖµ¡£
Listing 14-11 shows the contents of the Index.cshtml file from the Views/RoleAdmin folder, which I created by right-clicking the Index action method in the code editor and selecting Add View from the pop-up menu.
Çåµ¥14-11ÏÔʾÁËViews/RoleAdminÎļþ¼ÐÖÐIndex.cshtmlÎļþµÄÄÚÈÝ£¬ÕâÊÇͨ¹ýÔÚ´úÂë±à¼Æ÷ÖÐÓÒ»÷Index¶¯×÷£¬²¢´Óµ¯³ö²Ëµ¥ÖÐÑ¡Ôñ“Add View£¨Ìí¼ÓÊÓͼ£©”À´´´½¨µÄ¡£
Listing 14-11. The Contents of the Index.cshtml File in the Views/RoleAdmin Folder
Çåµ¥14-11. Views/RoleAdminÎļþ¼ÐÖÐIndex.cshtmlÎļþµÄÄÚÈÝ
<div> <div>Roles</div> <table>
<tr><th>ID</th><th>Name</th><th>Users</th><th></th></tr> @if (Model.Count() == 0) { <tr><td colspan="4">No Roles</td></tr> } else { foreach (AppRole role in Model) { <tr> <td>@role.Id</td> <td>@role.Name</td> <td> @if (role.Users == null || role.Users.Count == 0) { @: No Users in Role } else { <p>@string.Join(", ", role.Users.Select(x => Html.GetUserName(x.UserId)))</p> } </td> <td> @using (Html.BeginForm("Delete", "RoleAdmin", new { id = role.Id })) { @Html.ActionLink("Edit", "Edit", new { id = role.Id }, new { @class = "btn btn-primary btn-xs" }) <button type="submit"> Delete </button> } </td> </tr> } } </table> </div> @Html.ActionLink("Create", "Create", null, new { @class = "btn btn-primary" })
This view displays a list of the roles defined by the application, along with the users who are members, and I use the GetUserName helper method to get the name for each user.
¸ÃÊÓͼÏÔʾÁËÒ»¸öÓÉÓ¦ÓóÌÐò¶¨ÒåµÄ½ÇÉ«ÁÐ±í£¬ÇÒ´øÓгÉÔ±Óû§£¬ÎÒÓÃGetUserName¸¨ÖúÆ÷·½·¨»ñÈ¡ÁËÿ¸öÓû§µÄÓû§Ãû¡£
Listing 14-12 shows the Views/RoleAdmin/Create.cshtml file, which I created to allow new roles to be created.
Çåµ¥14-12ÏÔʾÁËViews/RoleAdmin/Create.cshtmlÎļþ£¬ÕâÊÇÓÃÀ´´´½¨Ð½ÇÉ«µÄÊÓͼ¡£
Listing 14-12. The Contents of the Create.cshtml File in the Views/RoleAdmin Folder
Çåµ¥14-12. Views/RoleAdminÎļþ¼ÐÖÐCreate.cshtmlÎļþµÄÄÚÈÝ
The only information required to create a new view is a name, which I gather using a standard input element and submit the value to the Create action method.
´´½¨¸ÃÊÓͼÐèÒªµÄΨһÐÅÏ¢ÊǽÇÉ«Ãû£¬ÎÒÓñê×¼µÄinputÔªËؽøÐвɼ¯£¬²¢½«¸ÃÖµµÝ½»¸øCreate¶¯×÷·½·¨¡£
14.3.4 ²âÊÔ½ÇÉ«µÄ´´½¨ºÍɾ³ý
To test the new controller, start the application and navigate to the /RoleAdmin/Index URL. To create a new role, click the Create button, enter a name in the input element, and click the second Create button. The new view will be saved to the database and displayed when the browser is redirected to the Index action, as shown in Figure 14-4. You can remove the role from the application by clicking the Delete button.
ΪÁ˲âÊÔеĿØÖÆÆ÷£¬Æô¶¯Ó¦ÓóÌÐò²¢µ¼º½µ½/RoleAdmin/Index URL¡£ÎªÁË´´½¨Ò»¸öеĽÇÉ«£¬µã»÷“Create”°´Å¥£¬ÔÚinputÔªËØÖÐÊäÈëÒ»¸ö½ÇÉ«Ãû£¬È»ºóµã»÷µÚ¶þ¸ö“Create”°´Å¥¡£Ð½ÇÉ«½«±»±£´æµ½Êý¾Ý¿â£¬²¢ÔÚä¯ÀÀÆ÷±»Öض¨Ïòµ½Index¶¯×÷ʱÏÔʾ³öÀ´£¬Èçͼ14-4Ëùʾ¡£¿ÉÒÔµã»÷“Delete”°´Å¥½«¸Ã½ÇÉ«´ÓÓ¦ÓóÌÐòÖÐɾ³ý¡£
Figure 14-4. Creating a new role
ͼ14-4. ´´½¨Ð½ÇÉ«
14.3.5 ¹ÜÀí½ÇÉ«³ÉÔ±
¡¡