Ajax Grid.MVC的分页不工作
我在MVC web应用程序中使用Grid.MVC 当使用index controller在空白页中测试它时,它可以成功地进行分页和过滤。 当我把它放在我的项目中时,问题就产生了 我所做的步骤是向方法发出ajax请求(因为我不需要重新加载页面),并返回包含网格搜索结果的部分视图。Mvc成功返回结果和页数,但当我按“下一页”或“筛选”时,它不起作用 代码: 视图:Ajax Grid.MVC的分页不工作,ajax,asp.net-mvc,gridview,kendo-grid,Ajax,Asp.net Mvc,Gridview,Kendo Grid,我在MVC web应用程序中使用Grid.MVC 当使用index controller在空白页中测试它时,它可以成功地进行分页和过滤。 当我把它放在我的项目中时,问题就产生了 我所做的步骤是向方法发出ajax请求(因为我不需要重新加载页面),并返回包含网格搜索结果的部分视图。Mvc成功返回结果和页数,但当我按“下一页”或“筛选”时,它不起作用 代码: 视图: @使用(Ajax.BeginForm(“搜索”、“主页”), 新选择 { HttpMethod=“POST”, InsertionMod
@使用(Ajax.BeginForm(“搜索”、“主页”),
新选择
{
HttpMethod=“POST”,
InsertionMode=InsertionMode.Replace,
UpdateTargetId=“SearchResult”
})){
@Html.DropDownList(“省”、“省”)
@Html.DropDownList(“城市”、“城市”)
价格来源:
致:
}
搜索控制器:
[HttpPost]
public ActionResult Search(int? page , int Province = 0, int Cities = 0, int Pricefrom = 0, int Priceto = 0)
{
var ads = db.Ad.Where(a => (Cities == 0 || a.CityId == Cities) &&
(Province == 0 || a.Cities.ProvinceId == Province)&&
(Pricefrom == 0 || a.Price >= Pricefrom)&&
(Priceto == 0 || a.Price <= Priceto)).OrderBy(a => a.AdDate).ToList();
return PartialView("_Search", ads);
}
[HttpPost]
公共行动结果搜索(int?页,int省=0,int城市=0,int价格从=0,int价格到=0)
{
var ads=db.Ad.Where(a=>(Cities==0 | | a.CityId==Cities)&&
(省==0 | a.Cities.ProvinceId==省)&&
(Pricefrom==0 | a.Price>=Pricefrom)&&
(Priceto==0 | | a.Price a.AdDate).ToList();
返回PartialView(“搜索”,ads);
}
部分观点:
@using GridMvc.Html
@model IEnumerable<Semsark.Areas.Backend.Models.Ad>
<div>
@Html.Grid(Model).Columns(columns =>
{
columns.Add(c => c.Id).Titled("ID");
columns.Add(c => c.AdTitle).Titled("title");
columns.Add(c => c.AdBody).Titled("body");
}).WithPaging(2).Sortable(true)
</div>
@使用GridMvc.Html
@模型IEnumerable
@Html.Grid(Model).Columns(Columns=>
{
columns.Add(c=>c.Id).标题为(“Id”);
columns.Add(c=>c.AdTitle).title(“title”);
添加(c=>c.AdBody)。标题为(“body”);
}).WithPaging(2).可排序(true)
View index.cshtml中的脚本和样式:
<head>
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
<script src="~/Scripts/gridmvc.lang.ru.js"></script>
<title>Index</title>
指数
提前感谢您的帮助,您是否尝试过通过IQueryable列表而不是IEnumerable?根据文档,Gridmvc.Html需要使用IQueryable进行分页。IQueryable和IEnumerable之间存在一些细微的差异,这可能会造成不同。分页需要这些脚本:
"~/Scripts/GridMvc/URI.js"
"~/Scripts/GridMvc/gridmvc.min.js"
"~/Scripts/GridMvc/gridmvc-ext.js"
@*在MVC4中使用分页的Webgrid。
查看页面**.cshtml***@
@模型MvcPopup.Models.PagedEmployeeModel
@{
//ViewBag.Title=“SearchEmployee”;
布局=空;
}
@{
WebGrid=newwebgrid(rowsPerPage:Model.PageSize);
grid.Bind(Model.Employee,
autoSortAndPage:false,
rowCount:Model.TotalRows
);
}
@grid.GetHtml(
fillEmptyRows:false,
交替行样式:“交替行”,
标题样式:“网格标题”,
页脚样式:“网格页脚”,
模式:WebGridPagerModes.All,
第一个文本:“,
lastText:“Last>>”,
列:新[]{
grid.Column(“名称”,
标题:“姓名”,
格式:@
@ActionLink((string)item.Name,“ViewEmployeeDetail”,新的{id=item.id},新的{@class=“viewDialog”})
),
网格栏(“部门”),
网格栏(“城市”),
网格栏(“状态”),
grid.Column(“国家”,
标题:“国家”
),
网格柱(“移动”),
网格.列(“”,
标题:“行动”,
格式:@
@ActionLink(“Edit”、“EditEmployee”、新的{id=item.id}、新的{@class=“editDialog”})
|
@ActionLink(“删除”,“删除”,新建{id=item.id},新建{@class=“confirmDialog”})
)
})
@*-----------------------------------
modelservices.cs文件下的模型文件夹
=================================*@
公共IEnumerable GetEmployeePage(int pageNumber、int pageSize、字符串搜索条件)
{
如果(页码<1)
页码=1;
返回db.Employees
.OrderBy(m=>m.Name)
.Skip((页码-1)*页面大小)
.Take(页面大小)
.ToList();
}
公共int CountalEmployee()
{
返回db.Employees.Count();
}
公共类PagedEmployeeModel
{
公共整数TotalRows{get;set;}
公共IEnumerable Employee{get;set;}
公共int PageSize{get;set;}
}
@*创建文件(如employeecontroller.cs)下的控制器文件夹*@
使用MvcPopup.Models;
利用制度全球化;
使用系统文本;
命名空间MvcPopup.Controllers
{
公共类EmployeeController:控制器
{
//
//获取:/Employee/
ModelServices mobjModel=newmodelservices();
公共行动结果索引()
{
返回视图();
}
public ActionResult SearchEmployee(int page=1,string sort=“name”,string sortDir=“ASC”)
{
常量int pageSize=5;
var totalRows=mobjModel.CountAllEmployee();
sortDir=sortDir.Equals(“desc”,StringComparison.CurrentCultureInogoreCase)?sortDir:“asc”;
var validColumns=new[]{“id”、“name”、“department”、“country”};
if(!validColumns.Any(c=>c.Equals(sort,StringComparison.CurrentCultureIgnoreCase)))
sort=“id”;
var employee=mobjModel.GetEmployeePage(页面,页面大小,“it.”+排序+“”+sortDir);
var data=new pagedeEmployeeModel()
{
TotalRows=TotalRows,
PageSize=PageSize,
雇员=雇员
};
"~/Scripts/GridMvc/URI.js"
"~/Scripts/GridMvc/gridmvc.min.js"
"~/Scripts/GridMvc/gridmvc-ext.js"
@*Webgrid using Paging in mvc 4.
View Page **.cshtml** *@
@model MvcPopup.Models.PagedEmployeeModel
@{
//ViewBag.Title = "SearchEmployee";
Layout = null;
}
@{
WebGrid grid = new WebGrid(rowsPerPage: Model.PageSize);
grid.Bind(Model.Employee,
autoSortAndPage: false,
rowCount: Model.TotalRows
);
}
@grid.GetHtml(
fillEmptyRows: false,
alternatingRowStyle: "alternate-row",
headerStyle: "grid-header",
footerStyle: "grid-footer",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("Name",
header: "Name",
format: @<text>
@Html.ActionLink((string)item.Name, "ViewEmployeeDetail", new { id = item.id }, new { @class = "viewDialog" })</text>
),
grid.Column("Department"),
grid.Column("City"),
grid.Column("State"),
grid.Column("Country",
header: "Country"
),
grid.Column("Mobile"),
grid.Column("",
header: "Actions",
format: @<text>
@Html.ActionLink("Edit", "EditEmployee", new { id = item.id }, new { @class = "editDialog" })
|
@Html.ActionLink("Delete", "Delete", new { id = item.id }, new { @class = "confirmDialog"})
</text>
)
})
@*-----------------------------------
Model folder under modelservices.cs file
=================================*@
public IEnumerable<Employee> GetEmployeePage(int pageNumber, int pageSize, string searchCriteria)
{
if (pageNumber < 1)
pageNumber = 1;
return db.Employees
.OrderBy(m =>m.Name)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
}
public int CountAllEmployee()
{
return db.Employees.Count();
}
public class PagedEmployeeModel
{
public int TotalRows { get; set; }
public IEnumerable<Employee> Employee { get; set; }
public int PageSize { get; set; }
}
@*Controller folder under create file like employeecontroller.cs *@
using MvcPopup.Models;
using System.Globalization;
using System.Text;
namespace MvcPopup.Controllers
{
public class EmployeeController : Controller
{
//
// GET: /Employee/
ModelServices mobjModel = new ModelServices();
public ActionResult Index()
{
return View();
}
public ActionResult SearchEmployee(int page = 1, string sort = "name", string sortDir = "ASC")
{
const int pageSize = 5;
var totalRows = mobjModel.CountAllEmployee();
sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
var validColumns = new[] { "id", "name", "department", "country" };
if (!validColumns.Any(c => c.Equals(sort, StringComparison.CurrentCultureIgnoreCase)))
sort = "id";
var employee = mobjModel.GetEmployeePage(page, pageSize, "it." + sort + " " + sortDir);
var data = new PagedEmployeeModel()
{
TotalRows = totalRows,
PageSize = pageSize,
Employee = employee
};
return View(data);
}
------------------------