這篇文章將為大家詳細(xì)講解有關(guān)制作MVC4分頁(yè)控件的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
新蔡ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!上一次做分頁(yè)控件的時(shí)候設(shè)想的把分頁(yè)設(shè)置類保存到數(shù)據(jù)庫(kù),后來(lái)覺得的沒必要這么做。分頁(yè)的包括htmlhelper 數(shù)據(jù)模型和分頁(yè)設(shè)置都在PagerExtensions.cs中,不跟數(shù)據(jù)庫(kù)發(fā)生關(guān)系,當(dāng)其他項(xiàng)目中需要用分頁(yè)的時(shí)候直接拷貝這個(gè)文件過(guò)去就可以直接用。欄目中的分頁(yè)設(shè)置直接在欄目中添加字段然后在控制器中new一個(gè)PagerConfig,然后設(shè)置響應(yīng)值。
修改后的PagerConfig說(shuō)明
PagerConfig類
////// 分頁(yè)配置 /// public class PagerConfig { ////// 當(dāng)前頁(yè) /// public int CurrentPage { get; set; } ////// 每頁(yè)記錄數(shù) /// public int PageSize { get; set; } ////// 總頁(yè)數(shù) /// public int TotalPage { get { return (int)Math.Ceiling(TotalRecord / (double)PageSize); } } ////// 總記錄數(shù) /// public int TotalRecord { get; set; } ////// 記錄單位 /// public string RecordUnit { get; set; } ////// 記錄名稱 /// public string RecordName { get; set; } public PagerConfig() { CurrentPage = 1; PageSize = 20; RecordUnit = "條"; RecordName = "記錄"; } }
后面要修改欄目模型用來(lái)保存分頁(yè)設(shè)置
Category模型字段說(shuō)明
修改后的欄目模型類
////// 欄目模型 /// public class Category { [Key] [Display(Name = "欄目Id")] public int CategoryId { get; set; } ////// 欄目名稱 /// [Display(Name="欄目名稱",Description="2-20個(gè)字符")] [Required(ErrorMessage="×")] [StringLength(50,ErrorMessage="×")] public string Name { get; set; } ////// 父欄目編號(hào) /// [Display(Name="父欄目")] [Required(ErrorMessage="×")] public int ParentId { get; set; } ////// 父欄目路徑【根節(jié)點(diǎn)的值為0,子節(jié)點(diǎn)的值為:0,1,6,76】 /// [Required()] public string ParentPath { get; set; } ////// 欄目類型【0-常規(guī)欄目;1-單頁(yè)欄目;2-外部鏈接】 /// [Display(Name="欄目類型")] [Required(ErrorMessage = "×")] public int Type { get; set; } ////// 內(nèi)容模型【僅在欄目為普通欄目時(shí)有效】 /// [Display(Name="內(nèi)容模型")] [StringLength(50, ErrorMessage = "×")] public string Model { get; set; } ////// 欄目視圖 /// [Display(Name = "欄目視圖", Description = "欄目頁(yè)的視圖,最多255個(gè)字符。。")] [StringLength(255, ErrorMessage = "×")] public string CategoryView { get; set; } ////// 內(nèi)容頁(yè)視圖 /// [Display(Name = "內(nèi)容視圖", Description = "內(nèi)容頁(yè)視圖,最多255個(gè)字符。。")] [StringLength(255, ErrorMessage = "×")] public string ContentView { get; set; } ////// 鏈接地址 /// [Display(Name="鏈接地址",Description="點(diǎn)擊欄目時(shí)跳轉(zhuǎn)到的鏈接地址,最多255個(gè)字符。")] [StringLength(255,ErrorMessage = "×")] public string LinkUrl { get; set; } ////// 欄目排序 /// [Display(Name = "欄目排序", Description = "針對(duì)同級(jí)欄目,數(shù)字越小順序越靠前。")] [Required(ErrorMessage = "×")] public int Order { get; set; } ////// 內(nèi)容排序 /// [Display(Name = "內(nèi)容排序", Description = "欄目所屬內(nèi)容的排序方式。")] public int? ContentOrder { get; set; } ////// 每頁(yè)記錄數(shù) /// [Display(Name = "每頁(yè)記錄數(shù)", Description = "欄目所屬內(nèi)容的排序方式。")] public int? PageSize { get; set; } ////// 記錄單位 /// [Display(Name = "記錄單位", Description = "記錄的數(shù)量單位。如文章為“篇”;新聞為“條”。")] [StringLength(255, ErrorMessage = "×")] public string RecordUnit { get; set; } ////// 記錄名稱 /// [Display(Name = "記錄名稱", Description = "記錄的名稱。如“文章”、“新聞”、“教程”等。")] [StringLength(255, ErrorMessage = "×")] public string RecordName { get; set; } public Category() { ParentPath = "0"; Type = 0; CategoryView = "Index"; ContentView = "Index"; Order = 0; ContentOrder = 1; PageSize = 20; RecordUnit = "條"; RecordName = "篇"; } }
由于欄目的模型字段發(fā)生變換 添加 修改欄目信息的視圖和action也要進(jìn)行相應(yīng)修改。
修改ManageAdd視圖
@model Ninesky.Models.Category @{ ViewBag.Title = "ManageAdd"; Layout = "~/Views/Layout/_Manage.cshtml"; }添加欄目@using (Html.BeginForm()) { @Html.ValidationSummary(true) }@Html.Action("ManagePartialTree", "Category")@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
修改ManageAdd(Category category),主要是首先驗(yàn)證選定的父欄目是否存在。根據(jù)選定的欄目類型驗(yàn)證相應(yīng)的字段是否輸入,設(shè)置無(wú)關(guān)字段為null
[AdminAuthorize] [HttpPost] public ActionResult ManageAdd(Category category) { //父欄目是否存在 if (categoryRsy.Find(category.ParentId) == null) ModelState.AddModelError("ParentId", "父欄目不存在。"); //ParentPath if (category.ParentId == 0) category.ParentPath = "0"; else category.ParentPath = categoryRsy.Find(category.ParentId).ParentPath + "," + category.ParentId; switch (category.Type) { case 0://常規(guī)欄目 if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "×"); category.LinkUrl = null; if (!string.IsNullOrEmpty(category.Model)) { if (string.IsNullOrEmpty(category.ContentView)) ModelState.AddModelError("ContentView", "×"); if (category.ContentOrder == null) category.ContentOrder = 0; if (category.PageSize == null) category.PageSize = 20; if (string.IsNullOrEmpty(category.RecordUnit)) category.RecordUnit = "條"; if (string.IsNullOrEmpty(category.RecordName)) category.RecordName = "記錄"; } else { category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; } break; case 1://單頁(yè)欄目 if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "×"); category.LinkUrl = null; category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; break; case 2://外部鏈接 if (string.IsNullOrEmpty(category.LinkUrl)) ModelState.AddModelError("LinkUrl", "×"); category.CategoryView = null; category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; break; default: ModelState.AddModelError("Type", "×"); break; } if (ModelState.IsValid) { if (categoryRsy.Add(category)) { Notice _n = new Notice { Title = "添加欄目成功", Details = "您已經(jīng)成功添加[" + category.Name + "]欄目!", DwellTime = 5, NavigationName = "欄目列表", NavigationUrl = Url.Action("ManageDefault", "Category") }; return RedirectToAction("ManageNotice", "Prompt", _n); } else { Error _e = new Error { Title = "添加欄目失敗", Details = "在添加欄目時(shí),未能保存到數(shù)據(jù)庫(kù)", Cause = "系統(tǒng)錯(cuò)誤", Solution = Server.UrlEncode("
下面就該欄目詳細(xì)信息的視圖ManageDetails.cshtml,基本與ManageAdd視圖。
然后修改更新欄目信息action [ManageUpdate(Category category)]。
在這里1、要檢查父欄目是不是其本身或其子欄目,如果是添加驗(yàn)證未通過(guò)信息。2、根據(jù)選定的欄目類型驗(yàn)證相應(yīng)的字段是否輸入,設(shè)置無(wú)關(guān)字段為null。3、如果父欄目發(fā)生更還,更改其本身及其子欄目的ParentPath。
////// 修改欄目信息 /// /// ///public ActionResult ManageUpdate(Category category) { //父欄目不能為本身或子欄目 if (categoryRsy.IsSelfOrLower(category.CategoryId,category.ParentId)) ModelState.AddModelError("ParentId", "父欄目不能是其本身或其子欄目"); switch (category.Type) { case 0://常規(guī)欄目 if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "×"); category.LinkUrl = null; if (!string.IsNullOrEmpty(category.Model)) { if (string.IsNullOrEmpty(category.ContentView)) ModelState.AddModelError("ContentView", "×"); if (category.ContentOrder == null) category.ContentOrder = 0; if (category.PageSize == null) category.PageSize = 20; if (string.IsNullOrEmpty(category.RecordUnit)) category.RecordUnit = "條"; if (string.IsNullOrEmpty(category.RecordName)) category.RecordName = "記錄"; } else { category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; } break; case 1://單頁(yè)欄目 if (string.IsNullOrEmpty(category.CategoryView)) ModelState.AddModelError("CategoryView", "×"); category.LinkUrl = null; category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; break; case 2://外部鏈接 if (string.IsNullOrEmpty(category.LinkUrl)) ModelState.AddModelError("LinkUrl", "×"); category.CategoryView = null; category.ContentView = null; category.ContentOrder = null; category.PageSize = null; category.RecordUnit = null; category.RecordName = null; break; default: ModelState.AddModelError("Type", "×"); break; } if (ModelState.IsValid) { var _pId = categoryRsy.Find(category.CategoryId).ParentId; var _oldParentPath = categoryRsy.Find(category.CategoryId).ParentPath + "," + category.CategoryId; //父欄目發(fā)生更改 if (category.ParentId != _pId) { //ParentPath if (category.ParentId == 0) category.ParentPath = "0"; else category.ParentPath = categoryRsy.Find(category.ParentId).ParentPath + "," + category.ParentId; } if (categoryRsy.Update(category)) { Notice _n = new Notice { Title = "修改欄目成功", Details = "修改欄目成功!", DwellTime = 5, NavigationName = "欄目詳細(xì)信息", NavigationUrl = Url.Action("ManageDetails", "Category", new { id = category.CategoryId }) }; if (_oldParentPath != category.ParentPath) { //修改子欄目ParentPath categoryRsy.UpdateCategorysParentPath(_oldParentPath, category.ParentPath+"," + category.CategoryId); } return RedirectToAction("ManageNotice", "Prompt", _n); } else { Error _e = new Error { Title = "修改欄目失敗", Details = "在修改欄目信息時(shí),未能保存到數(shù)據(jù)庫(kù)", Cause = "系統(tǒng)錯(cuò)誤", Solution = Server.UrlEncode("
關(guān)于“制作MVC4分頁(yè)控件的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。