MVC4做網站后台:欄目管理3、刪除欄目與左側列表菜單


一、左側列表菜單

打開視圖Menu.cshtml,增加部分見紅框image

在category中添加腳本

//欄目菜單加載完畢函數
function CategoryMenu_Ready() {
    $('#categoryTreeView').tree({
        url: $('#categoryTreeView').attr('data-url'),
        lines:true,
        onClick: function (node) {
            var _layout = $('#layout');
            var _center = _layout.layout('panel', 'center');
            _center.panel('refresh','/Admin/Category/Modify/' + node.id);
        }
    });
}

完工。

image

 

二、刪除欄目。

在category控制器中添加刪除action。action中有三不刪:欄目不存在不能刪(沒法刪);有子欄目不能刪(刪了會亂套);欄目有內容不能刪(保留);

代碼如下

/// <summary>
        /// 刪除欄目
        /// </summary>
        /// <param name="id">欄目Id</param>
        /// <returns>Json類型數據</returns>
        [HttpPost]
        public JsonResult Del(int id)
        {
            JsonViewModel _jsongviewModel = new JsonViewModel(){ Authentication=0, ValidationList= new Dictionary<string,string>()};
            //欄目不存在
            if (categoryRepository.Find(id) == null)
            {
                _jsongviewModel.Success = false;
                _jsongviewModel.Message = "欄目不存在,請確認欄目是否已經刪除。";
            }
            //存在子欄目
            else if (categoryRepository.Children(id).Count() > 0)
            {
                _jsongviewModel.Success = false;
                _jsongviewModel.Message = "該欄目存在子欄目,請先刪除子欄目。";
            }
            //判斷是否存在內容(預留)

            //執行刪除
            else
            {

                if (categoryRepository.Delete(id))
                {
                    _jsongviewModel.Success = true;
                    _jsongviewModel.Message = "刪除成功。";
                }
                else
                {
                    _jsongviewModel.Success = false;
                    _jsongviewModel.Message = "未知錯誤,未能從數據庫中刪除欄目。";
                }
            }
            return Json(_jsongviewModel);
        }

打開Modify.cshtml視圖,在修改按鈕的旁邊加刪除按鈕

<a id="CategoryModify_Delete" href="javascript:void()" onclick="CategoryDel('@Url.Action("Del","Category")',@Model.CategoryId)" class="easyui-linkbutton">刪除</a>

打開category.js文件。添加CategoryDel()函數

//刪除欄目
function CategoryDel(url, id) {
    if (confirm("你確定要刪除此欄目嗎?")) {
        $.post(url, { Id: id }, function (data) {
            //驗證
            if (data.Authentication == 0) {
                //操作成功
                if (data.Success) {
                    $.messager.alert("刪除欄目成功", data.Message, "info");
                    if ($('#categoryTreeView') != undefined) {
                        $('#categoryTreeView').tree('reload');
                    }
                }
                else {
                    $.messager.alert("刪除欄目失敗", data.Message, "error");
                }
            }
            else AuthenticationFailed(data.Authentication);
        }, "json");
    }
}

完工。

image

代碼見網盤或群Ninesky2013-12-06.rar


免責聲明!

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



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