自定义Ajax绑定无法正常工作

时间:2013-08-05 17:32:51

标签: asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid telerik-mvc

我有以下自定义Ajax绑定代码。即使显示第一页的数据,也存在以下问题。

request.Sorts在Orders_Read方法中显示为NULL

request.PageSize为Orders_Read方法

的0

•Orders_Read方法的request.Page为1(即使我点击了第2页)

这里需要做哪些更改才能获得正确的排序和页面化值?

注意:我正在使用MVC Wrapper for Kendo Grid。

查看

@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"))
 )
)
}

控制器

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; }
}
}

1 个答案:

答案 0 :(得分:2)

我遇到了和你一样的问题,最后在调查问题后花了很多时间才找到解决方案。如果其他人遇到同样的问题,我会在这里发布。

您需要从参数中删除前缀

public ActionResult Orders_Read([DataSourceRequest(Prefix = "grid12")]DataSourceRequest request)

转换为:

public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)

我不知道这是否是来自剑道的错误!但在此场景中,无法通过Prefix定义找到网格。

您可以找到示例here