自阿贾克斯绑定不能正常工作 [英] Custom Ajax Binding does not work properly
问题描述
我有以下code自定义的Ajax绑定。这具有以下即使它的第一页中显示的数据的问题。
• request.Sorts
即将为NULL到Orders_Read方法
• request.PageSize
0为即将在Orders_Read方法
• request.Page
为1来到Orders_Read方法(即使我点击第2页)
这里需要做什么样的变化,以得到适当的排序和页面大小值?
请注意:我使用MVC包装的剑道电网
查看
@ Scripts.Render(〜/包/ jQuery的)<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$('#Submit1')。点击(函数(){
警报('1');
$('#grid12')的数据(kendoGrid')dataSource.read()。
});
});
< / SCRIPT>
@model KendoPratapSampleMVCApp.Models.Sample
@ {
ViewBag.Title =CustomAjaxbind;
}< H2> CustomAjaxbind< / H>
@using(Html.BeginForm(PostValues,CustomAjaxBinding,FormMethod.Post))
{<输入ID =Submit1类型=按钮值=SubmitValue/>
@(Html.Kendo()网格和LT; KendoPratapSampleMVCApp.Models.Sample>()
。名称(grid12)
.EnableCustomBinding(真)
.Columns(列=> {
columns.Bound(p值=> p.SampleDescription).Filterable(假).WIDTH(100);
columns.Bound(P => p.Sample code).Filterable(假).WIDTH(100);
columns.Bound(p值=> p.SampleItems).Filterable(假).WIDTH(100);
})
.Pageable()
.Sortable()
.Scrollable()
.AutoBind(假)
.Filterable()
.HtmlAttributes(新{风格=高度:430px;})
.DataSource(数据源=>数据源
阿贾克斯()
.PageSize(2)
.Read(读=> read.Action(Orders_Read,CustomAjaxBinding))
)
)
}
控制器
公共类CustomAjaxBindingController:控制器
{
//
// GET:/ CustomAjaxBinding / 公众的ActionResult指数()
{
返回视图(CustomAjaxbind);
} 公众的ActionResult Orders_Read([DataSourceRequest(preFIX =grid12)] DataSourceRequest要求)
{ 字符串=的SortFieldSampleDescription;
字符串sortDirection =的String.Empty; 如果(request.Sorts!= NULL)
{
的foreach(SortDescriptor sortDescriptor在request.Sorts)
{
=的SortField sortDescriptor.Member;
如果(sortDescriptor.SortDirection == ListSortDirection.Ascending)
{
sortDirection =升序;
}
其他
{
sortDirection =降序;
}
}
} INT总= 1;
INT myPageSize = 2;
如果(request.PageSize!= 0)
{
myPageSize = request.PageSize;
} IEnumerable的<样品> currentSamples = GetSubsetEmployees(request.Page - 1,myPageSize,出总共的SortField,sortDirection); VAR的结果=新DataSourceResult()
{
数据= currentSamples,
总记录=总//总数
}; 返回JSON(结果);
} 公共IEnumerable的<样品> GetSubsetEmployees(INT的PageIndex,诠释的pageSize,OUT INT ITEMCOUNT,字符串的SortField,串sortDirection)
{ IEnumerable的<样品>样本= GetSamples();
ITEMCOUNT = samples.ToList()计数。 VAR选择=新的函数功能<样品,对象>(E =方式> e.GetType()的getProperty(的SortField).GetValue(即,NULL));
VAR的查询= sortDirection.Equals(降,StringComparison.OrdinalIgnoreCase)
? samples.OrderByDescending(选择)
:samples.OrderBy(选择); 清单<样品> currentPageEmployees =查询
.Skip(PageIndex的* pageSize的)
。取(pageSize的)
.ToList(); 返回currentPageEmployees; } 公共静态的IEnumerable<样品> GetSamples()
{
清单<样品> sampleAdd =新的List<样品>();
样品S12 =新样本();
s12.Sample code =1;
s12.SampleDescription =A;
s12.SampleItems =newone; 样品S2 =新样本();
s2.Sample code =2;
s2.SampleDescription =B;
s2.SampleItems =oldone; 样品S3 =新样本();
s3.Sample code =3;
s3.SampleDescription =C;
s3.SampleItems =latestone; 样品S4 =新样本();
s4.Sample code =4;
s4.SampleDescription =D;
s4.SampleItems =latestoneitem; sampleAdd.Add(S12);
sampleAdd.Add(S2);
sampleAdd.Add(S3);
sampleAdd.Add(S4);
返回sampleAdd;
}
}
型号
命名空间KendoUIMvcSample.Models
{
公共类SampleModel
{
公开名单<样品>样本;
}
公共类样品
{
公共字符串SampleDescription {搞定;组; }
公共字符串示例code {搞定;组; }
公共字符串SampleItems {搞定;组; }
}
}
我有同样的问题,因为你终于在调查问题耗费多少次后找到了解决办法最近。我张贴在这里,如果别人遇到了同样的问题。
您需要删除的 preFIX 从参数:
公众的ActionResult Orders_Read([DataSourceRequest(preFIX =grid12)] DataSourceRequest要求)
转换为:
公众的ActionResult Orders_Read([DataSourceRequest] DataSourceRequest要求)
我不知道这是不是从剑道中的错误或不!但是,在这种情况下的电网不能定义为preFIX被找到。
您可以找到一个例子<一个href=\"http://www.telerik.com/clientsfiles/081750a6-57f2-4106-8a6d-1787a0802214_custom-binding.rar?sfvrsn=0\"相对=nofollow>这里
I have following code for Custom Ajax Binding. This has following problems even though it is displaying data for the first page.
• The request.Sorts
is coming as NULL in to the Orders_Read method
• The request.PageSize
is coming as 0 to the Orders_Read method
• The request.Page
is coming as 1 to the Orders_Read method (even if I click on the page 2)
What changes need to be done here to get proper sort and pagesize values?
Note: I am using MVC Wrapper for Kendo Grid.
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;
}
}
Model
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; }
}
}
I had the same problem as yours and finally found the solution recently after consuming much times on investigating the problem. I post it here if someone else faced the same issue.
You need to remove Prefix from your parameter:
public ActionResult Orders_Read([DataSourceRequest(Prefix = "grid12")]DataSourceRequest request)
Converted to:
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
I don't know if this is a bug from Kendo or not! But in this scenario grid can't be find by Prefix defined.
You can find an example here
这篇关于自阿贾克斯绑定不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!