Kendo UI MVC - basic ajax binding

China☆狼群 提交于 2019-12-25 11:02:41

问题


I saw the example referred here

So in my application I try to implement it this way:

HomeController

public ActionResult About([DataSourceRequest]DataSourceRequest request)
    {
       List<ShortDetail> listSD = new List<ShortDetail>();
       ... fill the list with objects

       var v = listSD.ToDataSourceResult(request, sd => new ShortDetail { firstname = sd.firstname, surname = sd.surname, classname = sd.classname});

           return Json(v, JsonRequestBehavior.AllowGet)
     }

My Model ShortDetail

public class ShortDetail 
{
    public string firstname { get; set; }
    public string surname { get; set; }
    public int classid { get; set; }
    public string classname { get; set; }
    public string grade { get; set; }
    public int studentid { get; set; }
    public DateTime birthdate { get; set; }
    public int? indicatorID { get; set; }
    public string indicatorDescription { get; set; }
    public List<ResultMergedWithType> results { get; set; }

}

In my View About.cshtml

 <div class="col-md-12 table-responsive" id="mapsDiv">
   @(Html.Kendo().Grid<KendoExample.Models.ShortDetail>().Name("grid").DataSource(ds => ds.Ajax().Read(read => read.Action("About", "Home"))).Pageable())


</div>

Now i get raw json in browser

Alternatively if i try to bind the model with view

About.cshtml

@model List<KendoExample.Models.ShortDetail>
@using Kendo.Mvc.UI

 <div class="col-md-12 table-responsive" id="mapsDiv">
  @(Html.Kendo().Grid<KendoExample.Models.ShortDetail>().Name("grid").DataSource(ds => ds.Ajax().Read(read => read.Action("About", "Home"))).Pageable())


</div>

HomeController

 public ActionResult About([DataSourceRequest]DataSourceRequest request)
 {
   ...
   return View(listSD);
 }

then there is an empty grid with all columns as defined in model shortdetails except the ResultMergedWithType property


回答1:


I think you may have missed a step in the tutorial. You haven't declared your column schema (step 9) anywhere:

@(Html.Kendo().Grid<KendoExample.Models.ShortDetail>()
    .Name("grid")
    .DataSource(ds => ds.Ajax()
        .Read(read => read.Action("About", "Home")))
    .Columns(columns =>
    {
        columns.Bound(c => c.firstname);
        columns.Bound(c => c.surname); ... 
    }
    .Pageable())

This is required to bind the data to the relevant columns.




回答2:


change your code while you returning the result using json from return Json(v, JsonRequestBehavior.AllowGet) to return Json(v.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

for that you need to add a namespace using Kendo.Mvc.Extensions;




回答3:


I think @TechVision is correct. I haven't tested it but you may be using that extension method incorrectly. I have a similar setup and the only difference in the code is :

List<SomeObject> items =  new GetSomeObjects();
return Json(items.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

If you still get the error after the change above then you are probably using the extension method correctly and I would open up web debugger and look at the source of the request and see if you are calling your controller method somewhere else in your code like BeginForm() or elsewhere.




回答4:


I found what I was missing..we have to specify a data source in Grid<..>() constructor

About.cshtml

 @(Html.Kendo().Grid<KendoExample.Models.ShortDetail>(Model).Name("grid").DataSource(ds => ds.Ajax().Read(read => read.Action("About", "Home"))).Pageable())

HomeController

 public ActionResult About([DataSourceRequest]DataSourceRequest request)
 {
   ... 
   return View(listSD);
 }

It's ironic though that kendo support team was unable to help me with this problem.

Cheers!



来源:https://stackoverflow.com/questions/44276293/kendo-ui-mvc-basic-ajax-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!