濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1

MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1

熱門(mén)標(biāo)簽:地圖標(biāo)注位置怎么弄圖 威力最大的電銷(xiāo)機(jī)器人 電銷(xiāo)專(zhuān)用外呼線路 廣西房產(chǎn)智能外呼系統(tǒng)推薦 電銷(xiāo)外呼系統(tǒng)是違法的嗎 電話機(jī)器人鑰匙扣 400電話唐山辦理 旅游地圖標(biāo)注線路 漯河外呼調(diào)研線路

一、用戶(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();
 IQueryableUserGroup> _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 IEnumerableNinesky.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 ListSelectListItem> TypeSelectList
 {
 get
 {
 ListSelectListItem> _items = new ListSelectListItem>();
 _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();
 IQueryableUserGroup> _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 IEnumerableNinesky.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(六)

標(biāo)簽:試駕邀約 焦作 湘西 綏化 湖北 無(wú)錫 欽州 銅陵

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1》,本文關(guān)鍵詞  MVC4,制作,網(wǎng)站,教程,第三章,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MVC4制作網(wǎng)站教程第三章 瀏覽用戶(hù)組操作3.1的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    临颍县| 平塘县| 樟树市| 咸丰县| 商河县| 水富县| 淮阳县| 安福县| 闻喜县| 右玉县| 万盛区| 定结县| 汶上县| 库尔勒市| 琼结县| 恩施市| 双辽市| 锦屏县| 杭锦旗| 沙湾县| 河曲县| 沂南县| 岳普湖县| 赣榆县| 娱乐| 河间市| 清水县| 丹江口市| 哈巴河县| 虹口区| 仙居县| 渭南市| 龙江县| 洪泽县| 山西省| 丰镇市| 康乐县| 泾阳县| 伊金霍洛旗| 成武县| 永清县|