標籤:style blog io color ar os 使用 sp for
建立角色相關視圖
第一步:添加視圖 開啟RolesAdminController.cs 將滑鼠移動到public ActionResult Index()上 右鍵》添加視圖 系統會彈出對話方塊 什麼也不用改 直接“確定”
第二步:在建立的視圖上定義一個公開枚舉模型
在第一行添加 @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
第三步:建立整頁模式模板,程式碼完成後如下。
@model IEnumerable <Microsoft .AspNet.Identity.EntityFramework.IdentityRole>@{ ViewBag.Title = "角色列表";}<h2>角色列表</h2><p> @Html.ActionLink("建立角色", "Create")</p><table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.ActionLink("編輯角色", "Edit", new { id = item.Id }) | @Html.ActionLink("角色詳情", "Details", new { id = item.Id }) | @Html.ActionLink("刪除角色", "Delete", new { id = item.Id }) </td> </tr> }</table>
重複上述步驟完成其他視圖模板。
需要注意的是
1、Create視圖模板和Edit視圖模板 頂部定義的是一個xxxx(項目名).Models.RoleViewModel模型。
2、Delete視圖模板和Details視圖模板 頂部定義的是一個Microsoft.AspNet.Identity.EntityFramework.IdentityRole模型。
完成後的相關代碼如下:
Create視圖模板
@model xxxx(項目名).Models.RoleViewModel@{ ViewBag.Title = "建立角色";}<h2>建立角色</h2>@using (Html.BeginForm()){ @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Role.</h4> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextBoxFor(model => model.Name, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Name) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div>}<div> @Html.ActionLink("返回角色列表", "Index")</div>@section Scripts { @Scripts.Render("~/bundles/jqueryval")}
Edit視圖模板:
@model xxxx.Models.RoleViewModel@{ ViewBag.Title = "編輯角色";}<h2>編輯角色</h2>@using (Html.BeginForm()){ @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Roles.</h4> <hr /> @Html.ValidationSummary(true) @Html.HiddenFor(model => model.Id) <div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextBoxFor(model => model.Name, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Name) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div>}<div> @Html.ActionLink("返回角色列表", "Index")</div>@section Scripts { @Scripts.Render("~/bundles/jqueryval")}
Details視圖模板:
@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole@{ ViewBag.Title = "角色詳情";}<h2>角色詳情</h2><div> <h4>角色</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Name) </dt> <dd> @Html.DisplayFor(model => model.Name) </dd> </dl></div><h4>List of users in this role</h4>@if (ViewBag.UserCount == 0){ <hr /> <p>No users found in this role.</p>}<table class="table"> @foreach (var item in ViewBag.Users) { <tr> <td> @item.UserName </td> </tr> }</table><p> @Html.ActionLink("編輯角色", "Edit", new { id = Model.Id }) | @Html.ActionLink("返回角色列表", "Index")</p>
Delete視圖模板:
@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole@{ ViewBag.Title = "刪除角色";}<h2>刪除角色</h2><h3>Are you sure you want to delete this Role? </h3><p>Deleting this Role will remove all users from this role. It will not delete the users.</p><div> <h4>Role.</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Name) </dt> <dd> @Html.DisplayFor(model => model.Name) </dd> </dl> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-actions no-color"> <input type="submit" value="Delete" class="btn btn-default" /> | @Html.ActionLink("返回角色列表", "Index") </div> }</div>
至此,RoleAdmin的相關視圖模板就做完了。
asp.net identity 2.2.0 中角色啟用和基本使用(四)