MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1
一、用戶(hù)
二、用戶(hù)組
2.1瀏覽用戶(hù)組
在開(kāi)始做瀏覽用戶(hù)組之前,首先要考慮權(quán)限問(wèn)題。瀏覽、添加、修改、刪除用戶(hù)組必須是系統(tǒng)管理員才能進(jìn)行的操作,Action上必須驗(yàn)證是否是管理員,因此添加一個(gè)AdminAuthorize。在Extensions文件夾上點(diǎn)右鍵添加類(lèi)"AdminAuthorizeAttribute”,繼承自AuthorizeAttribute。
重寫(xiě)AuthorizeCore(HttpContextBase httpContext),里面什么代碼都不寫(xiě)直接返回true。
因?yàn)楣芾韱T這塊的功能還沒(méi)做,目的是不驗(yàn)證管理員就可以進(jìn)行添加、刪除、瀏覽,權(quán)限驗(yàn)證代碼等以后寫(xiě)管理員這塊時(shí)再加。
using System; namespace System.Web.Mvc { /// <summary> /// 管理員權(quán)限驗(yàn)證 /// </summary> public class AdminAuthorizeAttribute:AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { return true; } } }
修改[List]Action,給其加上管理員權(quán)限驗(yàn)證。
/// <summary> /// 用戶(hù)組列表 /// </summary> /// <param name="Id">用戶(hù)組類(lèi)型</param> /// <returns></returns> [AdminAuthorize] public ActionResult List(int Id = -1) { userGroupRsy = new UserGroupRepository(); IQueryable<UserGroup> _userGroup; if (Id == -1) _userGroup = userGroupRsy.List(); else _userGroup = userGroupRsy.List(Id); return View(_userGroup); }
id是用戶(hù)組類(lèi)型,因?yàn)橛脩?hù)組類(lèi)型是枚舉類(lèi)型,從0起始,所以這里瀏覽地址不帶id參數(shù)時(shí)設(shè)為-1顯示所有用戶(hù)組,當(dāng)如數(shù)id參數(shù)時(shí)顯示指定類(lèi)型的用戶(hù)組。
右鍵添加強(qiáng)類(lèi)型“UserGroup”視圖List.cshtml,修改生成的代碼。
@model IEnumerable<Ninesky.Models.UserGroup> @{ ViewBag.Title = "用戶(hù)組列表"; Layout = "~/Views/Layout/_Manage.cshtml"; } <div class="left"> <div class="top"></div> 左側(cè)列表 </div> <div class="split"></div> <div class="workspace"> <div class="inside"> <div class="notebar"> <img alt="" src="~/Skins/Default/Manage/Images/UserGroup.gif" />用戶(hù)組列表 </div> <div class="buttonbar">@Html.ActionLink("添加用戶(hù)組", "Add", "UserGroup") </div> <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Type) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Type) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.ActionLink("修改", "Edit", new { id = item.UserGroupId }) | @Html.ActionLink("刪除", "Delete", new { id = item.UserGroupId }) </td> </tr> } </table> </div> </div> <div class="clear"></div>
運(yùn)行瀏覽器里看下效果,還行。
現(xiàn)在應(yīng)該添加一個(gè)下拉菜單,可以選擇不同的用戶(hù)組類(lèi)型來(lái)顯示相應(yīng)類(lèi)型的用戶(hù)組
在【UserGroupController】添加屬性TypeSelectList
/// <summary> /// 用戶(hù)組類(lèi)型的SelectList列表 /// </summary> public List<SelectListItem> TypeSelectList { get { List<SelectListItem> _items = new List<SelectListItem>(); _items.Add(new SelectListItem { Text = UserGroupType.Anonymous.ToString(), Value = ((int)UserGroupType.Anonymous).ToString() }); _items.Add(new SelectListItem { Text = UserGroupType.Limited.ToString(), Value = ((int)UserGroupType.Limited).ToString() }); _items.Add(new SelectListItem { Text = UserGroupType.Normal.ToString(), Value = ((int)UserGroupType.Normal).ToString() }); _items.Add(new SelectListItem { Text = UserGroupType.Special.ToString(), Value = ((int)UserGroupType.Special).ToString() }); return _items; } }
修改[List]Action代碼
/// <summary> /// 用戶(hù)組列表 /// </summary> /// <param name="Id">用戶(hù)組類(lèi)型</param> /// <returns></returns> [AdminAuthorize] public ActionResult List(int Id = -1) { userGroupRsy = new UserGroupRepository(); IQueryable<UserGroup> _userGroup; if (Id == -1) _userGroup = userGroupRsy.List(); else _userGroup = userGroupRsy.List(Id); var _typeLists = TypeSelectList; _typeLists.Insert(0, new SelectListItem { Text = "全部", Value = "-1" }); if (_typeLists.Any(t => t.Value == Id.ToString())) _typeLists.SingleOrDefault(t => t.Value == Id.ToString()).Selected = true; ViewData.Add("GroupTypeList",_typeLists); return View(_userGroup); }
在L.cshtml視圖里@Html.ActionLink("添加用戶(hù)組", "Add", "UserGroup")后面添加
用戶(hù)組類(lèi)型:@Html.DropDownList("GroupTypeList")
底部添加
<script type="text/javascript"> $("#GroupTypeList").change(function () { window.location.href = "/UserGroup/List/" + $(this).children("option:selected").val(); }) </script>
完成后的List.cshtml代碼如下:
@model IEnumerable<Ninesky.Models.UserGroup> @{ ViewBag.Title = "用戶(hù)組列表"; Layout = "~/Views/Layout/_Manage.cshtml"; } <div class="left"> <div class="top"></div> 左側(cè)列表 </div> <div class="split"></div> <div class="workspace"> <div class="inside"> <div class="notebar"> <img alt="" src="~/Skins/Default/Manage/Images/UserGroup.gif" />用戶(hù)組列表 </div> <div class="buttonbar">@Html.ActionLink("添加用戶(hù)組", "Add", "UserGroup") 用戶(hù)組類(lèi)型: @Html.DropDownList("GroupTypeList") </div> <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Type) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Type) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.ActionLink("修改", "Edit", new { id = item.UserGroupId }) | @Html.ActionLink("刪除", "Delete", new { id = item.UserGroupId }) </td> </tr> } </table> </div> </div> <div class="clear"></div> <script type="text/javascript"> $("#GroupTypeList").change(function () { window.location.href = "/UserGroup/List/" + $(this).children("option:selected").val(); }) </script>
完成,瀏覽器中查看一下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)登錄、注銷(xiāo)(五)
- ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)注冊(cè)(四)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)概述(一)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架(二)
- ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)顯示文章列表(九)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)修改及刪除文章(十)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)添加文章(八)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)文章管理架構(gòu)(七)
- ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)咨詢(xún)管理的架構(gòu)(十一)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)之登錄、驗(yàn)證和注銷(xiāo)管理員篇1(六)
相關(guān)文章
你應(yīng)該知道的.NET錯(cuò)誤與異常處理機(jī)制
這篇文章主要給大家介紹了關(guān)于.NET錯(cuò)誤與異常處理機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08DataList綁定到Row[]行集合的問(wèn)題的方法
DataList綁定到Row[]行集合的問(wèn)題的方法...2007-09-09ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法,是asp.net操作office文件的一個(gè)典型應(yīng)用,代碼中備有較為詳盡的注釋便于讀者理解,需要的朋友可以參考下2014-11-11ASP.NET WebAPi(selfhost)實(shí)現(xiàn)文件同步或異步上傳
這篇文章主要介紹了ASP.NET WebAPi(selfhost)實(shí)現(xiàn)文件同步或異步上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11使用Visual Studio創(chuàng)建ASP.NET Web API項(xiàng)目
這篇文章介紹了使用Visual Studio創(chuàng)建ASP.NET Web API項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03輕量級(jí)ORM框架Dapper應(yīng)用之返回多個(gè)結(jié)果集
這篇文章介紹了使用Dapper返回多個(gè)結(jié)果集的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03用ASP.Net實(shí)現(xiàn)文件的在線壓縮和解壓縮
用ASP.Net實(shí)現(xiàn)文件的在線壓縮和解壓縮...2006-09-09ASP.Net分頁(yè)的分頁(yè)導(dǎo)航實(shí)例
本文介紹了ASP.Net分頁(yè)的分頁(yè)導(dǎo)航實(shí)例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-10-10