文章出處
文章列表
接上節添加欄目。
修改欄目與添加欄目非常相似,主要區別在于先向視圖傳遞要修改欄目的model。另外在保存時比添加欄目驗證要更多一些。
1、要驗證父欄目不能是其本身;
2、父欄目不能是其子欄目;
3、父欄目不能是單頁欄目
4、父欄目不能是外部鏈接
……
另外還有一個ParentParth字段,如果父欄目修改了,其本身和所有子欄目的該字段都要修改。
那么先在控制器中添加一個局部視圖action
/// <summary> /// 修改欄目 /// </summary> /// <param name="id">欄目Id</param> /// <returns>局部視圖</returns> public PartialViewResult Modify(int id) { return PartialView(categoryRepository.Find(id)); }
再添加其視圖,內容基本同,添加欄目
@model Ninesky.Models.Category <div class="c_navbar">欄目管理 >> 修改欄目</div> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="fs_wapper"> <div class="header">修改欄目</div>@Html.HiddenFor(model => model.CategoryId) @Html.ValidationSummary() <table class="fieldset"> <tr> <th>@Html.LabelFor(model => model.Name)<span>*</span></th> <td>@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) @Html.DisplayDescriptionFor(model => model.Name)</td> </tr> <tr> <th>@Html.LabelFor(model => model.ParentId)<span>*</span></th> <td> <input id="ParentId" name="ParentId" style="width: 200px;" data-url="@Url.Action("Tree", "Category")" value="@Model.ParentId" /> @Html.ValidationMessageFor(model => model.ParentId) </td> </tr> <tr> <th>@Html.LabelFor(model => model.Type)<span>*</span></th> <td> <input id="Type" name="Type" value="@Model.Type" /> @Html.ValidationMessageFor(model => model.Type) </td> </tr> <tr> <th>@Html.LabelFor(model => model.Model)<span>*</span></th> <td> <input id="Model" name="Model" value="@Model.Model" data-url="@Url.Action("Items", "Module", new { id = 1 })" /> @Html.ValidationMessageFor(model => model.Model) @Html.DisplayDescriptionFor(model => model.Model) </td> </tr> <tr> <th>@Html.LabelFor(model => model.CategoryView)<span>*</span></th> <td> <input id="CategoryView" class="easyui-combobox" name="CategoryView" data-options="valueField:'Name',textField:'Name',url:'@Url.Action("Views", "View", new { controllerName="Category" })'" value="@Model.CategoryView"> @Html.ValidationMessageFor(model => model.CategoryView) </td> </tr> <tr> <th>@Html.LabelFor(model => model.ContentView)<span>*</span></th> <td> <input id="ContentView" class="easyui-combobox" name="ContentView" data-url="@Url.Action("Views", "View")" data-options="valueField:'Name',textField:'Name',url:'@Url.Action("Views", "View", new { controllerName="Article" })'" value="@Model.ContentView"> @Html.ValidationMessageFor(model => model.ContentView) </td> </tr> <tr> <th>@Html.LabelFor(model => model.LinkUrl)<span>*</span></th> <td> @Html.EditorFor(model => model.LinkUrl) @Html.ValidationMessageFor(model => model.LinkUrl) @Html.DisplayDescriptionFor(model => model.LinkUrl) </td> </tr> <tr> <th>@Html.LabelFor(model => model.Order)<span>*</span></th> <td> @Html.EditorFor(model => model.Order) @Html.ValidationMessageFor(model => model.Order) @Html.DisplayDescriptionFor(model => model.Order) </td> </tr> <tr> <th>@Html.LabelFor(model => model.ContentOrder)<span>*</span></th> <td> <input id="ContentOrder" name="ContentOrder" class="easyui-combobox" style="width: 200px;" data-options="url:'@Url.Action("OrderList", "Item", new {area="" })',valueField:'Value',textField:'Name'" value="@Model.ContentOrder" /> @Html.ValidationMessageFor(model => model.ContentOrder) @Html.DisplayDescriptionFor(model => model.ContentOrder) </td> </tr> <tr> <th>@Html.LabelFor(model => model.PageSize)<span>*</span></th> <td> @Html.EditorFor(model => model.PageSize) @Html.ValidationMessageFor(model => model.PageSize) @Html.DisplayDescriptionFor(model => model.PageSize) </td> </tr> <tr> <th>@Html.LabelFor(model => model.RecordUnit)<span>*</span></th> <td> @Html.EditorFor(model => model.RecordUnit) @Html.ValidationMessageFor(model => model.RecordUnit) @Html.DisplayDescriptionFor(model => model.RecordUnit) </td> </tr> <tr> <th>@Html.LabelFor(model => model.RecordName)<span>*</span></th> <td> @Html.EditorFor(model => model.RecordName) @Html.ValidationMessageFor(model => model.RecordName) @Html.DisplayDescriptionFor(model => model.RecordName) </td> </tr> <tr> <th></th> <td> <a id="CategoryModify_Save" href="javascript:void()" class="easyui-linkbutton">修改</a> </td> </tr> </table> </div> @Scripts.Render("~/Areas/Admin/Scripts/Category.js"); <script type="text/javascript"> CategoryModify_Ready(); </script> @Scripts.Render("~/bundles/jqueryval")
js代碼與add類似,粘貼了,看代碼吧。
效果
重點是,后臺處理代碼。
首先考慮在父欄目修改時要更改其子欄目的所有ParentParth字段。
首先打開Ninesky.Areas.Admin.Repository.InterfaceCategory接口。
添加函數
/// <summary> /// 修改子欄目父路徑 /// </summary> /// <param name="originalParth">原路徑</param> /// <param name="newParh">新路徑</param> /// <returns>布爾值。true表示添加成功,false表示失敗。</returns> bool ChangeChildrenParentParth(string originalParth, string newParh);
Ninesky.Areas.Admin.Repository.CategoryRepository寫其實現代碼。
public bool ChangeChildrenParentParth(string originalParth, string newParh) { using (NineskyContext _nineskyContext = new NineskyContext()) { var _children = _nineskyContext.Categorys.Where(c => c.ParentPath.IndexOf(originalParth) == 0); foreach(var _child in _children) { _child.ParentPath = _child.ParentPath.Replace(originalParth, newParh); } return _nineskyContext.SaveChanges() > 0; } }
下面就是做控制器的修改處理action了。都寫了注釋就不解釋了。
[HttpPost] public JsonResult Modify(Category category) { JsonViewModel _jdata = new JsonViewModel(); if (ModelState.IsValid)//模型驗證通過 { var _category = categoryRepository.Find(category.CategoryId); //父欄目 if (category.ParentId == 0) category.ParentPath = "0"; else { var _parentCategory = categoryRepository.Find(category.ParentId); if (_parentCategory == null) ModelState.AddModelError("ParentId", "父欄目不存在。"); else if (_parentCategory.Type != 0) ModelState.AddModelError("ParentId", "父欄目不是常規欄目,不能添加子欄目。"); //此處驗證父欄目不能是其本身 else if (category.ParentId == category.CategoryId) ModelState.AddModelError("ParentId", "父欄目不能是其本身。"); //此處驗證父欄目不能是其子欄目 else if (_parentCategory.ParentPath.IndexOf(_category.ParentPath + "," + _category.CategoryId) == 0) ModelState.AddModelError("ParentId", "父欄目不能是其子欄目。"); //設置父欄目路徑 else category.ParentPath = _parentCategory.ParentPath + "," + _parentCategory.CategoryId; } //根據欄目類型驗證字段 switch (category.Type) { case 0://常規欄目 if (string.IsNullOrEmpty(category.Model))//模型為空 { category.ContentView = category.LinkUrl = category.RecordUnit = category.RecordName = null; category.ContentOrder = category.PageSize = null; } else { if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "欄目視圖不能為空。"); if (string.IsNullOrEmpty(category.ContentView)) ModelState.AddModelError("ContentView", "內容視圖不能為空。"); if (category.ContentOrder == null) ModelState.AddModelError("ContentOrder", "內容排序方式不能為空。"); if (category.PageSize == null) ModelState.AddModelError("PageSize", "每頁記錄數不能為空。"); if (string.IsNullOrEmpty(category.RecordUnit)) ModelState.AddModelError("RecordUnit", "記錄單位不能為空。"); if (string.IsNullOrEmpty(category.RecordName)) ModelState.AddModelError("RecordName", "記錄名稱不能為空。"); category.LinkUrl = null; } break; case 1://單頁欄目 if (categoryRepository.Children(category.CategoryId).Count() != 0) ModelState.AddModelError("Type", "存在子欄目,無法將欄目類型設為單頁欄目。"); if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "欄目視圖不能為空。"); category.Model = category.ContentView = category.LinkUrl = category.RecordUnit = category.RecordName = null; category.ContentOrder = category.PageSize = null; break; case 2: if (categoryRepository.Children(category.CategoryId).Count() != 0) ModelState.AddModelError("Type", "存在子欄目,無法將欄目類型設為外部鏈接。"); if (string.IsNullOrEmpty(category.LinkUrl)) ModelState.AddModelError("LinkUrl", "鏈接地址不能為空。"); category.Model = category.CategoryView = category.ContentView = category.RecordUnit = category.RecordName = category.ContentView = null; category.ContentOrder = category.PageSize = null; break; } //存在邏輯驗證錯誤 if (!ModelState.IsValid) return Json(new JsonViewModel(ModelState)); else { if (categoryRepository.Modify(category)) { //判斷父欄目是否更改 if (category.ParentId != _category.ParentId) categoryRepository.ChangeChildrenParentParth(_category.ParentPath + "," + category.CategoryId, category.ParentPath + "," + category.CategoryId); return Json(new JsonViewModel() { Success = true, Message = "修改欄目成功!" }); } else return Json(new JsonViewModel() { Success = false, Message = "數據未發生更改。" }); } } //模型驗證失敗 else return Json(new JsonViewModel(ModelState)); }
代碼見網盤或群Ninesky2013-12-06.rar
文章列表
全站熱搜