MVC无刷新查询,PagedList分页控件使用,导出Excel
使用MVC开发也有一段时间了,总结下无刷新部分视图的使用、PagedList分页控件的使用。
@using PagedList
@model StaticPagedList<T>
<style type="text/css">
.tab-title {
background-color: #efefef;
width: 10%;
} .btn-custom {
padding: 6px 24px !important;
color: #ffffff !important;
border-radius: 5px;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.04em;
}
</style>
@{ }
<section>
<div class="container" style="margin-top:10px;padding-top:2em;padding-bottom:4em;">
@using (Ajax.BeginForm("Index", "Hscode", null,
new AjaxOptions()
{
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "div-result",
OnComplete = "regJs"
},
new { @class = "form-horizontal", role = "form", id = "ajax-form" }))
{ <div class="form-group">
<div class="col-sm-4">
<input type="text" class="form-control" id="hscode" name="hscode" placeholder="商品编码">
</div>
<div class="col-sm-4">
<input type="text" class="form-control" id="gname" name="gname" placeholder="商品名称">
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-custom btn-sm">
<span class="glyphicon glyphicon-search"></span> 查询
</button>
<a href="javascript:void(0)" class="btn btn-custom btn-sm" id="btnExport"><span class="glyphicon glyphicon-download"></span> 导 出</a>
</div>
</div>
}
<div id="div-result">
@Html.Partial("_Index", Model)
</div>
</div>
</section> @section scripts{
<script src="~/Scripts/jquery.nicescroll.min.js"></script>
<script type="text/javascript">
function regJs() {
$("#pager > .pagination-container > .pagination > li > a").click(function () {
var pageUrl = $(this).attr("href");
var queryStr = $("#ajax-form").serialize();
$(this).attr("href", pageUrl + "&" + queryStr);
}); } regJs(); $("html").niceScroll({
cursorcolor: "#ddd",
cursoropacitymax: 1,
touchbehavior: false,
cursorwidth: "10px",
cursorborder: "0",
cursorborderradius: "5px"
}); $("#btnExport").click(function () {
var queryStr = $("#ajax-form").serialize();
location.href = "/Hscode/Export?" + queryStr;
});
</script>
}
主视图
- 使用@Html.Partial("_Index", Model)在主视图进行部分视图的渲染
- PagedList点击分页时如何提交查询条件?这个问题困扰了下我。
经网上查资料用如下方法解决:
$("#pager > .pagination-container > .pagination > li > a").click(function () {
var pageUrl = $(this).attr("href");
var queryStr = $("#ajax-form").serialize();
$(this).attr("href", pageUrl + "&" + queryStr);
});
即将表单内容通过URL查询字符串进行传递。
- 无刷新查询需要使用的js有:jquery.unobtrusive-ajax
- 无刷新查询后会导致js失效,使用OnComplete进行js方法的重新注册。
@using PagedList
@using PagedList.Mvc
@model StaticPagedList<Hscode> @if (Model != null && Model.Count > 0)
{ foreach (var hscode in Model)
{
<table class="table table-condensed">
<tbody>
<tr>
<td class="tab-title" style="border-top:1px dashed #000;">商品编码</td>
<td colspan="5" style="border-top:1px dashed #000;color:#ff0000;">@hscode.Hscode1</td>
</tr>
<tr>
<td class="tab-title">商品名称</td>
<td colspan="5">@hscode.GName</td>
</tr>
<tr>
<td class="tab-title">申报要素</td>
<td colspan="5">@hscode.DeclarationElements</td>
</tr>
<tr>
<td class="tab-title">法定第一单位</td>
<td>@hscode.Unit1</td>
<td class="tab-title">法定第二单位 </td>
<td>@hscode.Unit2</td>
<td colspan="2"></td> </tr>
<tr>
<td class="tab-title">最惠国进口税率</td>
<td>@hscode.ZHGJKSL</td>
<td class="tab-title">普通进口税率</td>
<td>@hscode.PTJKSL</td>
<td class="tab-title">暂定进口税率</td>
<td>@hscode.ZDJKSL</td>
</tr>
<tr>
<td class="tab-title">消费税率</td>
<td>@hscode.XFSL</td>
<td class="tab-title">出口关税率</td>
<td>@hscode.CKGSL</td>
<td class="tab-title">出口退税率</td>
<td>@hscode.CKTSL</td>
</tr>
<tr>
<td class="tab-title">增值税率</td>
<td>@hscode.ZZSL</td>
<td class="tab-title">海关监管条件</td>
<td>@hscode.HGJGTJ</td>
<td class="tab-title">检验检疫类别</td>
<td>@hscode.JYJYLB</td>
</tr>
<tr>
<td class="tab-title" style="border-bottom:1px dashed #000;">商品描述</td>
<td colspan="5" style="border-bottom:1px dashed #000;">@hscode.ProductDesc</td>
</tr>
</tbody>
</table>
} <div style="width:100%;text-align:center">每页 @Model.PageSize 条记录,共 @Model.PageCount 页,当前第 @Model.PageNumber 页,合计 @Model.TotalItemCount 条记录</div>
<div style="width:100%;text-align:center" id="pager">
@Html.PagedListPager(Model, page => Url.Action("Index", new
{
page
}), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions
{
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "div-result",
OnComplete = "regJs"
}))
</div>
}
else
{
<table class="table table-condensed">
<thead>
<tr>
<th class="text-center">不存在符合条件的数据!</th>
</tr>
</thead>
</table> }
部分视图
public class HscodeController : Controller
{
private BasicModels dbContext = new BasicModels(); public ActionResult Index(int? page)
{
return Query(page);
} [HttpPost]
public ActionResult Index(int? page, FormCollection form)
{
return Query(page, true);
} private ActionResult Query(int? page, bool ajaxQuery = false)
{
int pageIndex = page ?? ;
int pageSize = Pager.PageSize; IQueryable<Hscode> list = Query(); int totalItemCount = list.Count();
list = list.OrderBy(o => o.Id).Skip((pageIndex - ) * pageSize).Take(pageSize); StaticPagedList<Hscode> pageData = new StaticPagedList<Hscode>(list, pageIndex, pageSize, totalItemCount); if (ajaxQuery)
{
return PartialView("_Index", pageData);
}
else
{
return View("Index", pageData);
}
} private IQueryable<Hscode> Query()
{
IQueryable<Hscode> list = dbContext.Hscode; string hscode = Request["hscode"];
if (hscode != null && !string.IsNullOrEmpty(hscode.Trim()))
{
list = list.Where(w => w.Hscode1.Contains(hscode.Trim()));
} string gname = Request["gname"];
if (gname != null && !string.IsNullOrEmpty(gname.Trim()))
{
list = list.Where(w => w.GName.Contains(gname.Trim()));
} return list;
} public void Export()
{
IQueryable<Hscode> list = Query(); DataTable dt = list.Convert();
string strFileName = string.Format("申报要素_{0}", DateTime.Now.ToString("yyyyMMddHHmmssffffff")); ExcelHelper.ExportXlsxByWeb(dt, strFileName);
}
}
控制器代码
- 分页控件在视图中的使用如下
- 使用Entity Framework时,因为要根据查询条件进行查询。
早先使用IEnumerate<Hscode> list = dbContext.Hscode;导致表中全部内容加载到内存中,查询很慢。
怎么没使用延迟加载了?最后经同事指正如下才能做到延迟加载:
IQueryable<Hscode> list = dbContext.Hscode;
- 查询条件过滤数据
if (Request["hscode"] != null && !string.IsNullOrEmpty(Request["hscode"].Trim()))
{
list = list.Where(w => w.Hscode1.Contains(Request["hscode"].Trim()));
}
这种写法是有问题的。不知道大家开发的时候有没有遇到过?
有条件查询的时候会报错LINQ to Entities 不识别方法 System.String get_Item(System.String)
修正方法如下:
string hscode = Request["hscode"];
if (hscode != null && !string.IsNullOrEmpty(hscode.Trim()))
{
list = list.Where(w => w.Hscode1.Contains(hscode.Trim()));
}
MVC无刷新查询,PagedList分页控件使用,导出Excel的更多相关文章
- 【干货分享】JPager.Net MVC超好用轻量级分页控件
JPager.Net MVC好用的轻量级分页控件,好用到你无法想象,轻量到你无法想象. JPager.Net MVC好用的轻量级分页控件,实现非常简单,使用也非常简单. JPager.Net M ...
- 基于ASP.NET的MVC框架下的MvcPaper分页控件的使用技术
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Webdiyer. ...
- MVC中的自定义标签分页控件,仅供大家学习!!
public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, ...
- Net MVC轻量级分页控件
JPager.Net MVC超好用轻量级分页控件 JPager.Net MVC好用的轻量级分页控件,好用到你无法想象,轻量到你无法想象. JPager.Net MVC好用的轻量级分页控件,实现 ...
- 自己动手用Javascript写一个无刷新分页控件
.NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...
- MVC——分页控件
不管是什么类型的网站,分页都是必不可少的功能实现.在这里记录一下我自己接触过的分页控件: 一. MvcPager控件(记得项目里添加MvcPager.dll的引用) 这里面比较常用的就 ——@Html ...
- 基于存储过程的MVC开源分页控件--LYB.NET.SPPager
摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...
- 基于存储过程的MVC开源分页控件
基于存储过程的MVC开源分页控件--LYB.NET.SPPager 摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件M ...
- MVC无刷新分页
MVC无刷新分页(即局部刷新,带搜索,页数选择,排序功能) 我查看了很多网站,大部分评论分页都是局部刷新的,可大部分电商商品展示分页都是有刷新页面的,于是我便做了一个商品展示无刷新分页的例子.接下 ...
随机推荐
- NBUT 1457 Sona
莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> ...
- find命令--xargs--exec
find命令 1.1.find命令选项-name 按照文件名查找-perm 按照文件权限来查找-prune 可以使用find命令排除当前文件夹,不查找-user 可以按照文件属主来查找-group 可 ...
- Handler和Message以及Looper之间的三角关系
说到Handler想必大家都经常用到,在非UI线程更新UI那可是利器,用起来也非常容易上手 从使用上来说,我们只需要关注sendMessage和handleMessage即可 所以我们先从Handle ...
- iOS第三方常用类库
1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功能强大, 现在许多人 ...
- Tsinsen-1487:分配游戏【树状数组】
首先一定要看到x + y + z = N这个条件,没看到就世界再见了. 赢的人得分需要大于等于2,那么无非就是 (x, y), (x, z), (y, z), (x, y, z) 大于其他的点.但是考 ...
- [noip2013]货车运输(kruskal + 树上倍增)
描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多 ...
- java学习(四) excel读取
private static void readExcel() { String filePath = "C:/Standardzid.xls"; File file = new ...
- 快速搭建LNMP
[centos 6.4 server]系统安装请参考:http://blog.zhuyin.org/748.html1.防火墙设置: iptables -F service iptables save ...
- HTTP协议快速入门
一.定义 The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborativ ...
- Android滚动选择控件
现在觉得github特别方便,我一般直接使用github中的内容, https://github.com/wangjiegulu/WheelView 这里面都有详细的介绍