Asp.Net MVC Identity 2.2.1 使用技巧(七)

创建角色管理相关视图

1、添加视图

打开RolesAdminController.cs   将鼠标移动到public ActionResult Index()上  右键》添加视图   系统会弹出对话框  什么也不用改 直接“确定”。

2、在创建的视图上添加数据模型

 在第一行添加 @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>

3、建立Index页面视图模板,代码完成后如下:

 1 @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
 2 
 3 @{
 4     ViewBag.Title = "角色列表";
 5 }
 6 
 7 <h2>角色列表</h2>
 8 
 9 <p>
10     @Html.ActionLink("新建角色", "Create")
11 </p>
12 <table class="table">
13     <tr>
14         <th>
15             @Html.DisplayNameFor(model => model.Name)
16         </th>
17         <th>
18 
19         </th>
20     </tr>
21 
22     @foreach (var item in Model)
23     {
24         <tr>
25             <td>
26                 @Html.DisplayFor(modelItem => item.Name)
27             </td>
28             <td>
29                 @Html.ActionLink("编辑角色", "Edit", new { id = item.Id }) |
30                 @Html.ActionLink("角色详情", "Details", new { id = item.Id }) |
31                 @Html.ActionLink("删除角色", "Delete", new { id = item.Id })
32             </td>
33         </tr>
34     }
35 
36 </table>

重复上述步骤完成其他视图模板。

需要注意的是
1、Create视图模板和Edit视图模板 顶部定义的是一个xxxx(项目名).Models.RoleViewModel模型。
2、Delete视图模板和Details视图模板 顶部定义的是一个Microsoft.AspNet.Identity.EntityFramework.IdentityRole模型。

完成后的相关代码如下:

Details/角色详情视图

 1 @model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
 2 
 3 @{
 4     ViewBag.Title = "角色详情";
 5 }
 6 
 7 <h2>角色详情</h2>
 8 
 9 <div>
10     <h4>角色组</h4>
11     <hr />
12     <dl class="dl-horizontal">
13         <dt>
14             @Html.DisplayNameFor(model => model.Name)
15         </dt>
16         <dd>
17             @Html.DisplayFor(model => model.Name)
18         </dd>
19     </dl>
20 </div>
21 <h4>目前在该角色内的用户清单:</h4>
22 @if (ViewBag.UserCount == 0)
23 {
24     <hr />
25     <p>该角色未包含任何用户。</p>
26 }
27 
28 <table class="table">
29 
30     @foreach (var item in ViewBag.Users)
31     {
32         <tr>
33             <td>
34                 @item.UserName
35             </td>
36         </tr>
37     }
38 </table>
39 <p>
40     @Html.ActionLink("编辑角色", "Edit", new { id = Model.Id }) |
41     @Html.ActionLink("返回角色列表", "Index")
42 </p>

Create/创建角色视图

 1 @model xxxx.Models.RoleViewModel
 2 
 3 @{
 4     ViewBag.Title = "创建角色";
 5 }
 6 
 7 <h2>创建角色</h2>
 8 
 9 
10 @using (Html.BeginForm())
11 {
12     @Html.AntiForgeryToken()
13 
14     <div class="form-horizontal">
15         <h4>角色</h4>
16         <hr />
17         @Html.ValidationSummary(true)
18 
19         <div class="form-group">
20             @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
21             <div class="col-md-10">
22                 @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
23                 @Html.ValidationMessageFor(model => model.Name)
24             </div>
25         </div>
26 
27         <div class="form-group">
28             <div class="col-md-offset-2 col-md-10">
29                 <input type="submit" value="创建角色" class="btn btn-default" />
30             </div>
31         </div>
32     </div>
33 }
34 
35 <div>
36     @Html.ActionLink("返回角色列表", "Index")
37 </div>
38 
39 @section Scripts {
40     @Scripts.Render("~/bundles/jqueryval")
41 }

Edit/编辑角色视图

 1 @model xxxx.Models.RoleViewModel
 2 
 3 @{
 4     ViewBag.Title = "编辑角色";
 5 }
 6 
 7 <h2>编辑角色</h2>
 8 
 9 
10 @using (Html.BeginForm())
11 {
12     @Html.AntiForgeryToken()
13     
14     <div class="form-horizontal">
15         <h4>角色组</h4>
16         <hr />
17         @Html.ValidationSummary(true)
18         @Html.HiddenFor(model => model.Id)
19 
20         <div class="form-group">
21             @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
22             <div class="col-md-10">
23                 @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
24                 @Html.ValidationMessageFor(model => model.Name)
25             </div>
26         </div>
27 
28         <div class="form-group">
29             <div class="col-md-offset-2 col-md-10">
30                 <input type="submit" value="保存修改" class="btn btn-default" />
31             </div>
32         </div>
33     </div>
34 }
35 
36 <div>
37     @Html.ActionLink("返回角色列表", "Index")
38 </div>
39 
40 @section Scripts {
41     @Scripts.Render("~/bundles/jqueryval")
42 }

Delete/删除角色视图

 1 model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
 2 
 3 @{
 4     ViewBag.Title = "删除角色";
 5 }
 6 
 7 <h2>删除角色</h2>
 8 
 9 <h3>你确认要删除此角色??  </h3>
10 <p>该操作只能删除角色,但不会删除角色内的用户。</p>
11 <div>
12     <h4>角色</h4>
13     <hr />
14     <dl class="dl-horizontal">
15         <dt>
16             @Html.DisplayNameFor(model => model.Name)
17         </dt>
18 
19         <dd>
20             @Html.DisplayFor(model => model.Name)
21         </dd>
22     </dl>
23     @using (Html.BeginForm())
24     {
25         @Html.AntiForgeryToken()
26 
27         <div class="form-actions no-color">
28             <input type="submit" value="删除角色" class="btn btn-default" /> |
29             @Html.ActionLink("返回角色列表", "Index")
30         </div>
31     }
32 </div>
原文地址:https://www.cnblogs.com/chonghanyu/p/6392155.html