MVC4做網站後台:模組管理1、修改模組資訊

來源:互聯網
上載者:User

標籤:des   c   class   blog   code   java   

網站可能會包含一些模組:像文章、產品、圖片、留言等。

欄目模組主要實現功能,啟用或禁用模組,模組使用權限設定,模組上傳設定等。

使用權限設定和上傳設定以後專門考慮,先來顯示或禁用模組。

1、在頂部導覽列添加管理串連

開啟Home/Header.cshtml,在<nav>添加代碼

2、添加Module介面

using Ninesky.Models;using System.Linq;namespace Ninesky.Areas.Admin.Repository{    /// <summary>    /// 後台管理模組介面    /// <remarks>    /// 版本v1.0    /// 建立2013.12.10    /// </remarks>    /// </summary>    interface InterfaceModule:Ninesky.Repository.InterfaceModule    {        /// <summary>        /// 尋找模組        /// </summary>        /// <returns>所有模組列表</returns>        IQueryable<Module> Find();        /// <summary>        /// 修改模組資訊        /// </summary>        /// <param name="module">模組</param>        /// <returns>是否修改成功</returns>        bool Modify(Module module);    }}
3、介面執行個體
using Ninesky.Models;using System.Linq;namespace Ninesky.Areas.Admin.Repository{    /// <summary>    /// 模組介面執行個體    /// <remarks>    /// 版本v1.0    /// 建立2013.12.12    /// </remarks>    /// </summary>    public class ModuleRepository:Ninesky.Repository.ModuleRepository,InterfaceModule    {        public IQueryable<Module> Find()        {            return nContext.Modules;        }        public bool Modify(Module module)        {            nContext.Modules.Attach(module);            nContext.Entry<Module>(module).State = System.Data.EntityState.Modified;            return nContext.SaveChanges() > 0;        }    }}

4、添加控制器ModuleController

首先添加一個實現返回模組列表的action

/// <summary>        /// 列表        /// </summary>        /// <param name="enable">啟用狀態。【0-全部,1-啟用,2-禁用】</param>        /// <returns>        /// 普通請求時返回包含強型別List\<Module\>的視圖<br />        /// ajax請求返回json類型List\<Module\>        /// </returns>        public ActionResult Items(int id = 0)        {            var _modules = moduleRepository.Find();            if (Request.IsAjaxRequest()) return Json(_modules);            return View(_modules);        }

菜單

/// <summary>        /// 菜單        /// </summary>        /// <returns></returns>        public ActionResult Menu()        {            return PartialView(moduleRepository.Find());        }

菜單視圖

@model IEnumerable<Ninesky.Models.Module><div id="westmenu" class="easyui-accordion">    <div title="模組列表" class="leftsidebar">        <ul>            @foreach(var item in Model){            <li>@Html.ActionLink(item.Name, "Index", "Module", new { id = item.ModuleId }, new { @class = "westmenuitem" })</li>            }        </ul>    </div></div>@*@Scripts.Render("~/Areas/Admin/Scripts/Category.js")*@<script type="text/javascript">    WestMenu();</script>

模組資訊頁

/// <summary>        /// 模組資訊頁        /// </summary>        /// <param name="id"></param>        /// <returns></returns>        public ActionResult Index(int id)        {            return PartialView(moduleRepository.Find(id));        }
@model Ninesky.Models.Module<div class="c_navbar">後台管理 >> 模組管理 >> 模組資訊</div><div class="easyui-tabs">    <div title="基本資料">        <div class="fs_wapper">            @using (Html.BeginForm("Modify", "Module", FormMethod.Post, new { id = "module_base" }))            {                @Html.AntiForgeryToken()                @Html.ValidationSummary(true)                <div class="header">@Html.HiddenFor(model => model.ModuleId)</div>                @Html.ValidationSummary()                <table class="fieldset">                    <tr>                        <th>@Html.LabelFor(model => model.Name)<span>*</span></th>                        <td>@Html.TextBoxFor(model => model.Name, new { disabled = "disabled" })</td>                    </tr>                    <tr>                        <th>@Html.LabelFor(model => model.Enable)<span>*</span></th>                        <td>@Html.EditorFor(model => model.Enable)                            @Html.ValidationMessageFor(model => model.Description)                        </td>                    </tr>                    <tr>                        <th>@Html.LabelFor(model => model.Model)<span>*</span></th>                        <td>@Html.TextBoxFor(model => model.Model, new { disabled = "disabled" })</td>                    </tr>                    <tr>                        <th>@Html.LabelFor(model => model.Description)</th>                        <td>@Html.EditorFor(model => model.Description)                            @Html.ValidationMessageFor(model => model.Description)                        </td>                    </tr>                    <tr>                        <th></th>                        <td>                            <a id="btn_modulebase_save" class="easyui-linkbutton">儲存</a>                    </tr>                </table>             }            @Scripts.Render("~/Areas/Admin/Scripts/Module.js")            <script>                ModuleBase_Ready();            </script>        </div>    </div>    <div title="上傳設定">……</div>    <div title="使用權限設定">……</div></div>

這裡上傳設定和使用權限設定以後補充。

5、控制器中添加啟用、關閉模組處理action

/// <summary>        /// 修改模組資訊        /// </summary>        /// <returns></returns>        public ActionResult Modify(int? ModuleId,bool? Enable,string Description)        {            JsonViewModel _jsonViewModel = new JsonViewModel() { Authentication = 0, Success = true };            bool _modifyed = false;            if (ModuleId == null)             {                _jsonViewModel.Success = false;                _jsonViewModel.ValidationList.Add("ModuleId", "模組Id不可為空");            }            if (Enable == null)            {                _jsonViewModel.Success = false;                _jsonViewModel.ValidationList.Add("Enable", "啟用狀態不可為空");            }            if (_jsonViewModel.Success)            {                var _module = moduleRepository.Find((int)ModuleId);                if (_module == null)                {                    _jsonViewModel.Success = false;                    _jsonViewModel.Message = "模組存在";                }                else                {                    if (_module.Enable != Enable) { _module.Enable = (bool)Enable; _modifyed = true; }                    if (_module.Description != Description) { _module.Description = Description; _modifyed = true; }                    if (_modifyed)                    {                        if (moduleRepository.Modify(_module)) { _jsonViewModel.Success = true; _jsonViewModel.Message = "修改模組成功。"; }                        else { _jsonViewModel.Success = false; _jsonViewModel.Message = "資料未能儲存到資料庫"; }                    }                    else { _jsonViewModel.Success = false; _jsonViewModel.Message = "資料未發生更改"; }                }            }            else _jsonViewModel.Message = "資料驗證失敗!";            return Json(_jsonViewModel);        }

6、模組過濾器

這裡寫個過濾器,ModuleFilter。命名空間Ninesky.Extensions。作用是加到模組上面,當模組關閉時,前台使用者講不能訪問模組內容。

using Ninesky.Repository;using System.Web.Mvc;namespace Ninesky.Extensions{    /// <summary>    /// 模組過濾器    /// 屏蔽禁用模組    /// <remarks>    /// 版本v1.0    /// 建立2013.12.12    /// </remarks>    /// </summary>    public class ModuleFilter:ActionFilterAttribute    {        /// <summary>        /// 模型名        /// </summary>        public string Model { get; set; }        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            if (string.IsNullOrEmpty(Model)) filterContext.Result = new ContentResult() { Content = "未指定模組" };            switch (Model)            {                case "Article":                    InterfaceModule _moduleRepository = new ModuleRepository();                    var _module = _moduleRepository.Find(Model);                    if (_module == null) filterContext.Result = new ContentResult() { Content = "未找到指定模組" };                    else if (!_module.Enable) filterContext.Result = new ContentResult() { Content = _module.Name + "已關閉" };                    break;                default:                    filterContext.Result = new ContentResult() { Content = "未找到指定模組" };                    break;            }        }    }}

=================

代碼網盤或群Ninesky2013-12-12.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.