本篇文章為大家展示了MVC4制作網(wǎng)站在如何開(kāi)發(fā)瀏覽用戶(hù)組操作,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司專(zhuān)注于中大型企業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè)和網(wǎng)站改版、網(wǎng)站營(yíng)銷(xiāo)服務(wù),追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開(kāi)發(fā)的融合,累計(jì)客戶(hù)上1000家,服務(wù)滿(mǎn)意度達(dá)97%。幫助廣大客戶(hù)順利對(duì)接上互聯(lián)網(wǎng)浪潮,準(zhǔn)確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運(yùn)用,我們將一直專(zhuān)注成都品牌網(wǎng)站建設(shè)和互聯(lián)網(wǎng)程序開(kāi)發(fā),在前進(jìn)的路上,與客戶(hù)一起成長(zhǎng)!一、用戶(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>
完成,瀏覽器中查看一下
上述內(nèi)容就是MVC4制作網(wǎng)站在如何開(kāi)發(fā)瀏覽用戶(hù)組操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱(chēng):MVC4制作網(wǎng)站在如何瀏覽用戶(hù)組操作-創(chuàng)新互聯(lián)
文章起源:http://www.rwnh.cn/article38/djhgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、企業(yè)建站、響應(yīng)式網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容