MVC4制作网站教程第三章 浏览用户组操作3.1

一、用户

二、用户组

2.1浏览用户组

在开始做浏览用户组之前,首先要考虑权限问题。浏览、添加、修改、删除用户组必须是系统管理员才能进行的操作,Action上必须验证是否是管理员,因此添加一个AdminAuthorize。在Extensions文件夹上点右键添加类"AdminAuthorizeAttribute”,继承自AuthorizeAttribute。

重写AuthorizeCore(HttpContextBase httpContext),里面什么代码都不写直接返回true。

因为管理员这块的功能还没做,目的是不验证管理员就可以进行添加、删除、浏览,权限验证代码等以后写管理员这块时再加。

using System;

namespace System.Web.Mvc
{
 /// <summary>
 /// 管理员权限验证
 /// </summary>
 public class AdminAuthorizeAttribute:AuthorizeAttribute
 {
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {
 return true;
 }
 }
}

修改[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);
 return View(_userGroup);
 }

id是用户组类型,因为用户组类型是枚举类型,从0起始,所以这里浏览地址不带id参数时设为-1显示所有用户组,当如数id参数时显示指定类型的用户组。

右键添加强类型“UserGroup”视图List.cshtml,修改生成的代码。

@model IEnumerable<Ninesky.Models.UserGroup>

@{
 ViewBag.Title = "用户组列表";
 Layout = "~/Views/Layout/_Manage.cshtml";
}
<div class="left">
 <div class="top"></div>
 左侧列表
</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>

运行浏览器里看下效果,还行。

现在应该添加一个下拉菜单,可以选择不同的用户组类型来显示相应类型的用户组

在【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>
 左侧列表
</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>

完成,浏览器中查看一下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • ASP.NET MVC5网站开发修改及删除文章(十)

    上次做了显示文章列表,再实现修改和删除文章这部分内容就结束了,这次内容比较简单,由于做过了添加文章,修改文章非常类似,就是多了一个TryUpdateModel部分更新模型数据. 一.删除文章 由于公共模型跟,文章,附件有关联,所以这里的删除次序很重要,如果先删除模型,那么文章ModelID和附件的ModelID多会变成null,所以要先先删除文章和附件再删除公共模型. 由于公共模型和附件是一对多的关系,我们把删除公共模型和删除附件写在一起. 在BLL的BaseRepository类中有默认的De

  • ASP.NET MVC5网站开发添加文章(八)

    一.添加文章 1.KindEditor富文本编辑器 到官方网站http://kindeditor.net/down.php下载最新版本,解压后把代码复制到项目的Scripts文件夹下. 2.添加界面的显示. 在ArticleController中添加Add 方法 /// <summary> /// 添加文章 /// </summary> /// <returns>视图页面</returns> public ActionResult Add() { retur

  • ASP.NET MVC5网站开发项目框架(二)

    前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过"摸着石头过河"吧.这段时间看了一些博主的文章收获很大,特别是@kencery,依葫芦画瓢开写. 一.基本框架 还是先说下基本框架吧,一下子搞了7个项目看着挺乱的,我的理解是M.V.C 3者中,M是数据载体,V是用户要看的试图,C主要是协调控制与用户界面相关的操作,而数据的处理,数据库的的操作交给DAL.BLL来做.整个思路就是:View是用户看到的界面:Control

  • ASP.NET MVC5网站开发之登录、验证和注销管理员篇1(六)

    上次业务逻辑和展示层的架构都写了,可以开始进行具体功能的实现,这次先实现管理员的登录.验证和注销功能. 一.业务逻辑层1.实现256散列加密方法. Ninesky.Core[右键]-> 添加->文件夹,输入文件夹名General. General文件夹[右键]->添加->类,输入类名Security. 引用System.Security.Cryptography命名空间(1),并实现SHA256静态加密方法. 2.Administrator模型类 Ninesky.Core[右键]-

  • ASP.NET MVC5网站开发概述(一)

    前段时间一直在用MVC4写个网站开发的demo,由于刚开始学所有的代码都写在一个项目中,越写越混乱,到后来有些代码自己都理不清了.正好看到别人在用MVC5写东西,喜新厌旧的我马上下载了Visual Studio 2013,幸好MVC4到MVC5变化不大,这次准备用MVC5重新写个Demo. 每次看以前写的代码总有把它丢进回收站的冲动,其实没有完美的代码,能解决问题的代码就算是好代码吧,但是我还是决定重新写一个学习的Demo,希望这次能有提高,希望这次能写完吧! 一.开发环境 1.开发环境: Vi

  • ASP.NET MVC5网站开发显示文章列表(九)

    老习惯,先上个效果图: 1.在IBLL 在InterfaceCommonModelService接口中添加获取公共模型列表的方法 首先排序方法 /// <summary> /// 排序 /// </summary> /// <param name="entitys">数据实体集</param> /// <param name="roderCode">排序代码[默认:ID降序]</param> /

  • ASP.NET MVC5网站开发用户登录、注销(五)

    一.创建ClaimsIdentity ClaimsIdentity(委托基于声明的标识)是在ASP.NET Identity身份认证系统的登录时要用到,我们在UserService中来生成它. 1.打开IBLL项目InterfaceUserService接口,添加接口方法ClaimsIdentity CreateIdentity(User user, string authenticationType); 2.打开BLL项目的UserService类,添加CreateIdentity方法的实现代

  • ASP.NET MVC5网站开发文章管理架构(七)

    一.总体说明 先看一下文章管理设想要实现的功能: 再看一下类图 这里Category是栏目:CommonModel是公共模型:Article是文章:Attachment是附件: CommonModel是内容管理这块抽取出来的公共部分,像文章,咨询甚至产品都有一些共同的内容这里把它单独提取出来作为一个类.CommonModel可能包含一片文章,包含一组附件,包含一系列评论,他们之间的关系类图中已经表示出来.  二.搭建架构 这个顺序根以前一样 1.IDAL 在IDAL添加接口InterfaceCo

  • ASP.NET MVC5网站开发用户注册(四)

    一.默认Web项目的更改 用户这部分还是自己做,所以删除自动生成的用户相关代码. 二.添加Member区域 在web项目上点右键 添加 区域Member. 添加Home控制器,选择MVC5控制器-空 我们给public ActionResult Index()添加一个视图,代码很简单就是显示下用户名 @{ ViewBag.Title = "会员中心"; } <h2>欢迎你!@User.Identity.Name </h2> 我们先运行一下,出错啦. 这是因为项目

  • ASP.NET MVC5网站开发咨询管理的架构(十一)

    一.总体说明 1.实现功能 2.类图 由于文章部分把大部分类都是实现了,这里仅多了一个Consultation类. 二.创建咨询模型类 在Ninesky.Models项目添加类Consultation(咨询模型),该模型跟Article类似都是CommonModel的扩展. 1.添加Consultation类. using System; using System.ComponentModel.DataAnnotations; namespace Ninesky.Models { /// <su

随机推荐