学习制作MVC4分页控件(下)
程序员文章站
2023-12-03 15:16:04
上一次做分页控件的时候设想的把分页设置类保存到数据库,后来觉得的没必要这么做。分页的包括htmlhelper 数据模型和分页设置都在pagerextensions.cs中,...
上一次做分页控件的时候设想的把分页设置类保存到数据库,后来觉得的没必要这么做。分页的包括htmlhelper 数据模型和分页设置都在pagerextensions.cs中,不跟数据库发生关系,当其他项目中需要用分页的时候直接拷贝这个文件过去就可以直接用。栏目中的分页设置直接在栏目中添加字段然后在控制器中new一个pagerconfig,然后设置响应值。
修改后的pagerconfig说明
pagerconfig类
/// <summary> /// 分页配置 /// </summary> public class pagerconfig { /// <summary> /// 当前页 /// </summary> public int currentpage { get; set; } /// <summary> /// 每页记录数 /// </summary> public int pagesize { get; set; } /// <summary> /// 总页数 /// </summary> public int totalpage { get { return (int)math.ceiling(totalrecord / (double)pagesize); } } /// <summary> /// 总记录数 /// </summary> public int totalrecord { get; set; } /// <summary> /// 记录单位 /// </summary> public string recordunit { get; set; } /// <summary> /// 记录名称 /// </summary> public string recordname { get; set; } public pagerconfig() { currentpage = 1; pagesize = 20; recordunit = "条"; recordname = "记录"; } }
后面要修改栏目模型用来保存分页设置
category模型字段说明
修改后的栏目模型类
/// <summary> /// 栏目模型 /// </summary> public class category { [key] [display(name = "栏目id")] public int categoryid { get; set; } /// <summary> /// 栏目名称 /// </summary> [display(name="栏目名称",description="2-20个字符")] [required(errormessage="×")] [stringlength(50,errormessage="×")] public string name { get; set; } /// <summary> /// 父栏目编号 /// </summary> [display(name="父栏目")] [required(errormessage="×")] public int parentid { get; set; } /// <summary> /// 父栏目路径【根节点的值为0,子节点的值为:0,1,6,76】 /// </summary> [required()] public string parentpath { get; set; } /// <summary> /// 栏目类型【0-常规栏目;1-单页栏目;2-外部链接】 /// </summary> [display(name="栏目类型")] [required(errormessage = "×")] public int type { get; set; } /// <summary> /// 内容模型【仅在栏目为普通栏目时有效】 /// </summary> [display(name="内容模型")] [stringlength(50, errormessage = "×")] public string model { get; set; } /// <summary> /// 栏目视图 /// </summary> [display(name = "栏目视图", description = "栏目页的视图,最多255个字符。。")] [stringlength(255, errormessage = "×")] public string categoryview { get; set; } /// <summary> /// 内容页视图 /// </summary> [display(name = "内容视图", description = "内容页视图,最多255个字符。。")] [stringlength(255, errormessage = "×")] public string contentview { get; set; } /// <summary> /// 链接地址 /// </summary> [display(name="链接地址",description="点击栏目时跳转到的链接地址,最多255个字符。")] [stringlength(255,errormessage = "×")] public string linkurl { get; set; } /// <summary> /// 栏目排序 /// </summary> [display(name = "栏目排序", description = "针对同级栏目,数字越小顺序越靠前。")] [required(errormessage = "×")] public int order { get; set; } /// <summary> /// 内容排序 /// </summary> [display(name = "内容排序", description = "栏目所属内容的排序方式。")] public int? contentorder { get; set; } /// <summary> /// 每页记录数 /// </summary> [display(name = "每页记录数", description = "栏目所属内容的排序方式。")] public int? pagesize { get; set; } /// <summary> /// 记录单位 /// </summary> [display(name = "记录单位", description = "记录的数量单位。如文章为“篇”;新闻为“条”。")] [stringlength(255, errormessage = "×")] public string recordunit { get; set; } /// <summary> /// 记录名称 /// </summary> [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 = "篇"; } }
由于栏目的模型字段发生变换 添加 修改栏目信息的视图和action也要进行相应修改。
修改manageadd视图
@model ninesky.models.category @{ viewbag.title = "manageadd"; layout = "~/views/layout/_manage.cshtml"; } <div class="workspace"> <div class="inside"> <div class="notebar"> <img alt="" src="~/skins/default/manage/images/category.gif" />添加栏目 </div> @using (html.beginform()) { @html.validationsummary(true) <fieldset> <legend>栏目</legend> <ul> <li> <div class="editor-label"> @html.labelfor(model => model.type) </div> <div class="editor-field"> @html.dropdownlist("type") @html.validationmessagefor(model => model.type) @html.displaydescriptionfor(model => model.type) </div> </li> <li> <div class="editor-label"> @html.labelfor(model => model.name) </div> <div class="editor-field"> @html.editorfor(model => model.name) @html.validationmessagefor(model => model.name) @html.displaydescriptionfor(model => model.name) </div> </li> <li> <div class="editor-label"> @html.labelfor(model => model.parentid) </div> <div class="editor-field"> @html.textboxfor(model => model.parentid, new { @class = "easyui-combotree", data_options = "url:'" + url.action("jsontreeparent", "category") + "'" }) @html.validationmessagefor(model => model.parentid) @html.displaydescriptionfor(model => model.parentid) </div> </li> <li id="li_model"> <div class="editor-label"> @html.labelfor(model => model.model) </div> <div class="editor-field"> @html.dropdownlist("model") @html.validationmessagefor(model => model.model) @html.displaydescriptionfor(model => model.model) </div> </li> <li id="li_categoryview"> <div class="editor-label"> @html.labelfor(model => model.categoryview) </div> <div class="editor-field"> @html.editorfor(model => model.categoryview) @html.validationmessagefor(model => model.categoryview) @html.displaydescriptionfor(model => model.categoryview) </div> </li> <li id="li_contentview"> <div class="editor-label"> @html.labelfor(model => model.contentview) </div> <div class="editor-field"> @html.editorfor(model => model.contentview) @html.validationmessagefor(model => model.contentview) @html.displaydescriptionfor(model => model.contentview) </div> </li> <li id="li_url"> <div class="editor-label"> @html.labelfor(model => model.linkurl) </div> <div class="editor-field"> @html.editorfor(model => model.linkurl) @html.validationmessagefor(model => model.linkurl) @html.displaydescriptionfor(model => model.linkurl) </div> </li> <li> <div class="editor-label"> @html.labelfor(model => model.order) </div> <div class="editor-field"> @html.editorfor(model => model.order) @html.validationmessagefor(model => model.order) @html.displaydescriptionfor(model => model.order) </div> </li> <li id="li_corder"> <div class="editor-label"> @html.labelfor(model => model.contentorder) </div> <div class="editor-field"> @html.dropdownlist("contentorders") @html.validationmessagefor(model => model.contentorder) @html.displaydescriptionfor(model => model.contentorder) </div> </li> <li id="li_psize"> <div class="editor-label"> @html.labelfor(model => model.pagesize) </div> <div class="editor-field"> @html.editorfor(model => model.pagesize) @html.validationmessagefor(model => model.pagesize) @html.displaydescriptionfor(model => model.pagesize) </div> </li> <li id="li_runit"> <div class="editor-label"> @html.labelfor(model => model.recordunit) </div> <div class="editor-field"> @html.editorfor(model => model.recordunit) @html.validationmessagefor(model => model.recordunit) @html.displaydescriptionfor(model => model.recordunit) </div> </li> <li id="li_rname"> <div class="editor-label"> @html.labelfor(model => model.recordname) </div> <div class="editor-field"> @html.editorfor(model => model.recordname) @html.validationmessagefor(model => model.recordname) @html.displaydescriptionfor(model => model.recordname) </div> </li> <li> <div class="editor-label"> </div> <div class="editor-field"> <input type="submit" value="添加" /> </div> </li> </ul> </fieldset> } </div> </div> <div class="left"> <div class="top"></div> @html.action("managepartialtree", "category") </div> <div class="split"></div> <div class="clear"></div> <script type="text/javascript"> details(); $("#type").change(function () { details(); }); function details() { var v = $("#type").val(); if (v == "0") { $("#li_model").show(); $("#li_categoryview").show(); $("#li_contentview").show(); $("#li_url").hide(); $("#li_corder").show(); $("#li_psize").show(); $("#li_runit").show(); $("#li_rname").show(); } else if (v == "1") { $("#li_model").hide(); $("#li_categoryview").show(); $("#li_contentview").hide(); $("#li_url").hide(); $("#li_corder").hide(); $("#li_psize").hide(); $("#li_runit").hide(); $("#li_rname").hide(); } else if (v == "2") { $("#li_model").hide(); $("#li_categoryview").hide(); $("#li_contentview").hide(); $("#li_url").show(); $("#li_corder").hide(); $("#li_psize").hide(); $("#li_runit").hide(); $("#li_rname").hide(); } } </script> @section scripts { @scripts.render("~/bundles/jqueryval") }
修改manageadd(category category),主要是首先验证选定的父栏目是否存在。根据选定的栏目类型验证相应的字段是否输入,设置无关字段为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://常规栏目 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://单页栏目 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 = "您已经成功添加[" + category.name + "]栏目!", dwelltime = 5, navigationname = "栏目列表", navigationurl = url.action("managedefault", "category") }; return redirecttoaction("managenotice", "prompt", _n); } else { error _e = new error { title = "添加栏目失败", details = "在添加栏目时,未能保存到数据库", cause = "系统错误", solution = server.urlencode("<li>返回<a href='" + url.action("manageadd", "category") + "'>添加栏目</a>页面,输入正确的信息后重新操作</li><li>联系网站管理员</li>") }; return redirecttoaction("manageerror", "prompt", _e); } } else { modulerepository _modulersy = new modulerepository(); var _modules = _modulersy.list(true); list<selectlistitem> _slimodule = new list<selectlistitem>(_modules.count()); _slimodule.add(new selectlistitem { text = "无", value = "" }); foreach (module _module in _modules) { _slimodule.add(new selectlistitem { text = _module.name, value = _module.model }); } viewdata.add("model", _slimodule); viewdata.add("type", typeselectlist); viewdata.add("contentorders", commonmodel.contentorders); return view(category); } }
下面就该栏目详细信息的视图managedetails.cshtml,基本与manageadd视图。
然后修改更新栏目信息action [manageupdate(category category)]。
在这里1、要检查父栏目是不是其本身或其子栏目,如果是添加验证未通过信息。2、根据选定的栏目类型验证相应的字段是否输入,设置无关字段为null。3、如果父栏目发生更还,更改其本身及其子栏目的parentpath。
/// <summary> /// 修改栏目信息 /// </summary> /// <param name="category"></param> /// <returns></returns> public actionresult manageupdate(category category) { //父栏目不能为本身或子栏目 if (categoryrsy.isselforlower(category.categoryid,category.parentid)) modelstate.addmodelerror("parentid", "父栏目不能是其本身或其子栏目"); switch (category.type) { case 0://常规栏目 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://单页栏目 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; //父栏目发生更改 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 = "栏目详细信息", 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 = "在修改栏目信息时,未能保存到数据库", cause = "系统错误", solution = server.urlencode("<li>返回<a href='" + url.action("managedetails", "category", new { id = category.categoryid }) + "'>栏目详细资料</a>页面,修改信息后重新操作</li><li>联系网站管理员</li>") }; return redirecttoaction("manageerror", "prompt", _e); } } else { modulerepository _modulersy = new modulerepository(); var _modules = _modulersy.list(true); list<selectlistitem> _slimodule = new list<selectlistitem>(_modules.count()); _slimodule.add(new selectlistitem { text = "无", value = "" }); foreach (module _module in _modules) { _slimodule.add(new selectlistitem { text = _module.name, value = _module.model }); } viewdata.add("model", _slimodule); viewdata.add("type", typeselectlist); viewdata.add("contentorders", commonmodel.contentorders); return view("managedetails",category); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。