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

所属分类: 网络编程 / ASP.NET 阅读数: 1893
收藏 0 赞 0 分享

一、用户

二、用户组

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> 

完成,浏览器中查看一下

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

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多