學用MVC4做網站二:2.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>

完成,瀏覽器中查看一下

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM