自定义Ajax绑定无法正常工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Ajax绑定无法正常工作相关的知识,希望对你有一定的参考价值。
我有以下代码用于自定义Ajax绑定。即使它正在显示第一页的数据,也有以下问题。
•request.Sorts
作为NULL进入Orders_Read方法
•request.PageSize
作为0传递给Orders_Read方法
•request.Page
以1的形式出现在Orders_Read方法中(即使我单击第2页)
为了获得正确的排序和页面大小值,需要在此处进行哪些更改?
注意:我正在为Kendo Grid使用MVC包装器。
VIEW
@Scripts.Render("~/bundles/jquery")
<script type ="text/javascript">
$(document).ready(function ()
$('#Submit1').click(function ()
alert('1');
$('#grid12').data('kendoGrid').dataSource.read();
);
);
</script>
@model KendoPratapSampleMVCApp.Models.Sample
@
ViewBag.Title = "CustomAjaxbind";
<h2>CustomAjaxbind</h2>
@using (html.BeginForm("PostValues", "CustomAjaxBinding", FormMethod.Post))
<input id="Submit1" type="button" value="SubmitValue" />
@(Html.Kendo().Grid<KendoPratapSampleMVCApp.Models.Sample>()
.Name("grid12")
.EnableCustomBinding(true)
.Columns(columns =>
columns.Bound(p => p.SampleDescription).Filterable(false).Width(100);
columns.Bound(p => p.SampleCode).Filterable(false).Width(100);
columns.Bound(p => p.SampleItems).Filterable(false).Width(100);
)
.Pageable()
.Sortable()
.Scrollable()
.AutoBind(false)
.Filterable()
.HtmlAttributes(new style = "height:430px;" )
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(2)
.Read(read => read.Action("Orders_Read", "CustomAjaxBinding"))
)
)
Controller
public class CustomAjaxBindingController : Controller
//
// GET: /CustomAjaxBinding/
public ActionResult Index()
return View("CustomAjaxbind");
public ActionResult Orders_Read([DataSourceRequest(Prefix = "grid12")]DataSourceRequest request)
string sortField = "SampleDescription";
string sortDirection = string.Empty;
if (request.Sorts != null)
foreach (SortDescriptor sortDescriptor in request.Sorts)
sortField = sortDescriptor.Member;
if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
sortDirection = "Ascending";
else
sortDirection = "Descending";
int total = 1;
int myPageSize = 2;
if(request.PageSize !=0)
myPageSize = request.PageSize;
IEnumerable<Sample> currentSamples = GetSubsetEmployees(request.Page - 1, myPageSize, out total, sortField, sortDirection);
var result = new DataSourceResult()
Data = currentSamples,
Total = total // Total number of records
;
return Json(result);
public IEnumerable<Sample> GetSubsetEmployees(int pageIndex, int pageSize, out int itemCount, string sortField, string sortDirection)
IEnumerable<Sample> samples = GetSamples();
itemCount = samples.ToList().Count;
var selector = new Func<Sample, object>(e => e.GetType().GetProperty(sortField).GetValue(e, null));
var query = sortDirection.Equals("descending", StringComparison.OrdinalIgnoreCase)
? samples.OrderByDescending(selector)
: samples.OrderBy(selector);
List<Sample> currentPageEmployees = query
.Skip(pageIndex * pageSize)
.Take(pageSize)
.ToList();
return currentPageEmployees;
public static IEnumerable<Sample> GetSamples()
List<Sample> sampleAdd = new List<Sample>();
Sample s12 = new Sample();
s12.SampleCode = "1";
s12.SampleDescription = "A";
s12.SampleItems = "newone";
Sample s2 = new Sample();
s2.SampleCode = "2";
s2.SampleDescription = "B";
s2.SampleItems = "oldone";
Sample s3 = new Sample();
s3.SampleCode = "3";
s3.SampleDescription = "C";
s3.SampleItems = "latestone";
Sample s4 = new Sample();
s4.SampleCode = "4";
s4.SampleDescription = "D";
s4.SampleItems = "latestoneitem";
sampleAdd.Add(s12);
sampleAdd.Add(s2);
sampleAdd.Add(s3);
sampleAdd.Add(s4);
return sampleAdd;
模型
namespace KendoUIMvcSample.Models
public class SampleModel
public List<Sample> samples;
public class Sample
public string SampleDescription get; set;
public string SampleCode get; set;
public string SampleItems get; set;
答案
我遇到了与您相同的问题,经过花了几天的时间调查问题,终于在最近找到了解决方案。如果有人遇到同样的问题,我会在这里发布。
您需要从参数中删除Prefix:
public ActionResult Orders_Read([DataSourceRequest(Prefix = "grid12")]DataSourceRequest request)
转换为:
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
我不知道这是否是Kendo的错误!但是在这种情况下,无法通过前缀定义找到网格。
您可以找到示例here
以上是关于自定义Ajax绑定无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
将自定义图像 UIButtons 添加到自定义 UIToolbar 无法正常工作
使用自定义 AbstractAuthenticationProcessingFilter 和自定义 CustomAuthenticationProvider 的身份验证无法正常工作