MVC4制作網(wǎng)站教程第三章 瀏覽用戶組操作3.1
一、用戶
二、用戶組
2.1瀏覽用戶組
在開始做瀏覽用戶組之前,首先要考慮權(quán)限問(wèn)題。瀏覽、添加、修改、刪除用戶組必須是系統(tǒng)管理員才能進(jìn)行的操作,Action上必須驗(yàn)證是否是管理員,因此添加一個(gè)AdminAuthorize。在Extensions文件夾上點(diǎn)右鍵添加類"AdminAuthorizeAttribute”,繼承自AuthorizeAttribute。
重寫AuthorizeCore(HttpContextBase httpContext),里面什么代碼都不寫直接返回true。
因?yàn)楣芾韱T這塊的功能還沒做,目的是不驗(yàn)證管理員就可以進(jìn)行添加、刪除、瀏覽,權(quán)限驗(yàn)證代碼等以后寫管理員這塊時(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>
/// 用戶組列表
/// </summary>
/// <param name="Id">用戶組類型</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是用戶組類型,因?yàn)橛脩艚M類型是枚舉類型,從0起始,所以這里瀏覽地址不帶id參數(shù)時(shí)設(shè)為-1顯示所有用戶組,當(dāng)如數(shù)id參數(shù)時(shí)顯示指定類型的用戶組。
右鍵添加強(qiáng)類型“UserGroup”視圖List.cshtml,修改生成的代碼。
@model IEnumerable<Ninesky.Models.UserGroup>
@{
ViewBag.Title = "用戶組列表";
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" />用戶組列表
</div>
<div class="buttonbar">@Html.ActionLink("添加用戶組", "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è)下拉菜單,可以選擇不同的用戶組類型來(lái)顯示相應(yīng)類型的用戶組
在【UserGroupController】添加屬性TypeSelectList
/// <summary>
/// 用戶組類型的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>
/// 用戶組列表
/// </summary>
/// <param name="Id">用戶組類型</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("添加用戶組", "Add", "UserGroup")后面添加
用戶組類型:@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 = "用戶組列表";
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" />用戶組列表
</div>
<div class="buttonbar">@Html.ActionLink("添加用戶組", "Add", "UserGroup") 用戶組類型:
@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)站開發(fā)用戶登錄、注銷(五)
- ASP.NET?MVC5網(wǎng)站開發(fā)用戶注冊(cè)(四)
- ASP.NET MVC5網(wǎng)站開發(fā)概述(一)
- ASP.NET?MVC5網(wǎng)站開發(fā)項(xiàng)目框架(二)
- ASP.NET?MVC5網(wǎng)站開發(fā)顯示文章列表(九)
- ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
- ASP.NET MVC5網(wǎng)站開發(fā)添加文章(八)
- ASP.NET MVC5網(wǎng)站開發(fā)文章管理架構(gòu)(七)
- ASP.NET?MVC5網(wǎng)站開發(fā)咨詢管理的架構(gòu)(十一)
- ASP.NET MVC5網(wǎng)站開發(fā)之登錄、驗(yàn)證和注銷管理員篇1(六)
相關(guān)文章
Asp.net core中RedisMQ的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)
這篇文章主要介紹了Asp.net core中RedisMQ的簡(jiǎn)單應(yīng)用實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
ADO.NET獲取數(shù)據(jù)(DataSet)同時(shí)獲取表的架構(gòu)實(shí)例
下面小編就為大家分享一篇ADO.NET獲取數(shù)據(jù)(DataSet)同時(shí)獲取表的架構(gòu)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Entity?Framework?Core相關(guān)包的概念介紹與安裝
這篇文章介紹了Entity?Framework?Core相關(guān)包的概念與安裝方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Asp.Net Core中基于Session的身份驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core中基于Session的身份驗(yàn)證的實(shí)現(xiàn)2018-09-09
asp.net CheckBoxList各項(xiàng)最小寬度CSS樣式(兼容性good)
ASP.NET中,CheckBoxList里的選擇都是自動(dòng)寬度的,屬性時(shí)沒有設(shè)置各項(xiàng)寬度的設(shè)置,在IE10、遨游4極速模式及兼容模式下均可正確顯示最小寬度,此樣式除了用于CheckBoxList外,也可用于DIV等2013-04-04
asp.net core webapi項(xiàng)目配置全局路由的方法示例
這篇文章主要介紹了asp.net core webapi項(xiàng)目配置全局路由的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
asp.net 刪除項(xiàng)目文件/文件夾IIS重啟,Session丟失問(wèn)題
最近在做一個(gè)項(xiàng)目,涉及到大量文件中轉(zhuǎn)(先上傳到本項(xiàng)目的某個(gè)文件夾中,在移動(dòng)到FTP中),后面發(fā)現(xiàn)每次一刪除文件之后在做操作都會(huì)提示未登錄,剛開始以為是WebService Session丟失問(wèn)題,后面發(fā)現(xiàn)緩存也更新了2011-12-12

