Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use data from Model to bind as kendo datasource

i have an empty div that i want to initialize into a kendo grid using data from Model..it should be something like the following but i am unable to load data

$("#mapsDiv").kendoGrid({
    sortable: true,
    dataSource: {
                    transport: {
                                   read:"/Home/About",
                                   dataType: "odata"
                               },
                    pageSize: 5
                },
    pageable: true,
    resizable: true,
    columnMenu: true,
    scrollable:true,
    navigatable: true,
    editable: "incell"
});

About.cshtml

@model List<KendoExample.Entities.ShortStudent>

<div class="row">
<div class="col-md-12 table-responsive" id="mapsDiv">        
</div>

My Home Controller is as follows

List<ShortStudent> students = new List<ShortStudent>();

ShortStudent student1 = new ShortStudent();
student1.birthdate = new DateTime(1999, 4, 30);
student1.classname = "1B";
student1.firstname = "Fredie";
student1.surname = "Fletcher";
student1.studentid = 1;

ShortStudent student2 = new ShortStudent();
student2.birthdate = new DateTime(2010, 5, 4);
student2.classname = "1B";
student2.firstname = "Lee";
student2.surname = "Hobbs";
student2.studentid = 2;

students.Add(student1);
students.Add(student2);

return View(students);

I have seen examples using json but not odata...

Also, there are examples to use it like

@(Html.Kendo().Scheduler<MeetingViewModel>()
.Name("scheduler")
.Editable(false)
.DataSource(ds => ds
    .Custom()
    .Batch(true)
    .Schema(schema => schema
        .Model(m =>
        {
            m.Id(f => f.MeetingID);
            m.Field("title", typeof(string)).DefaultValue("No title").From("Title");
            m.Field("start", typeof(DateTime)).From("Start");
            m.Field("end", typeof(DateTime)).From("End");
            m.Field("description", typeof(string)).From("Description");
            m.Field("recurrenceID", typeof(int)).From("RecurrenceID");
            m.Field("recurrenceRule", typeof(string)).From("RecurrenceRule");
            m.Field("recurrenceException", typeof(string)).From("RecurrenceException");
            m.Field("isAllDay", typeof(bool)).From("IsAllDay");
            m.Field("startTimezone", typeof(string)).From("StartTimezone");
            m.Field("endTimezone", typeof(string)).From("EndTimezone");
        }))
    .Transport(new {
        //the ClientHandlerDescriptor is a special type that allows code rendering as-is (not as a string)
        read = new Kendo.Mvc.ClientHandlerDescriptor() {HandlerName = "customRead" }
    })
)
)

which i am unable to understand/implement so please ignore this kind of a solution.

Currently i see a grid footer that says (1 - 2 of 4852 items) without any header or content(datarows) on my screen. What am I doing wrong?

UPDATE

  var dataSource = new kendo.data.DataSource(
       {
           transport: {
               read: {
                   url: '@Url.Action("About", "Home")',
                   contentType: "application/json",
                   dataType: "json"
               }
           },
           schema: {
               model: {
                   fields: {
                       firstname: { type: "string" },
                       surname: { type: "string" },
                       birthdate: { type: "date" },
                       classname: { type: "string" }
                   }
               }
           },
           type: "json",
           serverPaging: false,
           serverFiltering: true,
           serverSorting: false
       }
    );

 $("#mapsDiv")
        .kendoGrid(
    {

        sortable: true,
        dataSource: {

            transport: {
                read: dataSource
            },
            pageSize: 2
        },
        pageable: true,
        resizable: false,
        columnMenu: true,
        scrollable:true,
        navigatable: true,
        editable: "incell",
        columns:[{
            field: "firstname",
        },{
            field: "surname",
        },{
            field: "classname",
        },{
            field: "age",
        }]
    });

HomeController

 public ActionResult About()
    {
   ....
     return View(students);
 }

Now the grid with header is there but no data is present.. If i change action to json, it returns plain json on the page

public ActionResult About()
    {
   ....
     return Json(students, JsonRequestBehavior.AllowGet);
 }
like image 594
Samra Avatar asked May 25 '17 02:05

Samra


People also ask

How do you bind model data to Kendo grid in MVC?

Bind data to Kendo Grid by using AJAX Read action method. Change the datasource on change event of any HTML controls. Normally, a developer can bind the data to Grid by using AJAX Read method. This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.

How do I change Kendo grid dataSource dynamically?

where dataString is of the format "{ column1: value1, column2: value2 }" or "[ { column1: value1, column2, value2 } ]" followed by: $(gridId). data('kendoGrid'). setDataSource(ds);


3 Answers

Have you tried adding the fields to the grid?

$("#mapsDiv")
    .kendoGrid(
{

    sortable: true,
    dataSource: {
        transport: {
           read:"/Home/About",
           dataType: "odata"
        },
        pageSize: 5
    },
                    columns: [
                        {
                            field: "classname",
                            title: "Class Name"
                        },
                        {
                            field: "firstname",
                            title: "First name"
                        },
                        {
                            field: "surname",
                            title: "Last name"
                        }
                    ],
    pageable: true,
    resizable: true,
    columnMenu: true,
    scrollable:true,
    navigatable: true,
    editable: "incell"

});
like image 151
Kris Avatar answered Oct 22 '22 14:10

Kris


I just visit demo of telerik. Try following. Hope to help, my friend. Or you can visit this link to refer more: http://demos.telerik.com/kendo-ui/grid/remote-data-binding.

$("#mapsDiv")
        .kendoGrid(
    {

        sortable: true,
        dataSource: {
            transport: {
               read:"/Home/About",
               dataType: "odata"
            },
            pageSize: 5
        },
       schema: {
                 model: {
                        fields: {
                             studentid: { type: "number" },
                             birthdate : { type: "date" },
                             classname : { type: "string" },
                             firstname : { type: "date" },
                             surname : { type: "string" }
                                    }
                                }
                            },
        pageable: true,
        resizable: true,
        columnMenu: true,
        scrollable:true,
        navigatable: true,
        editable: "incell"

    });
like image 43
Tomato32 Avatar answered Oct 22 '22 16:10

Tomato32


So here is what i found what should have been straight forward :)

var values = @Html.Raw(Json.Encode(@Model));

 $("#MapDetails")
        .kendoGrid(
    {
        sortable: true,
        dataSource: {
            data:values,
            pageSize: 2
        },
        pageable: true,
        resizable: false,
        columnMenu: true,
        scrollable:true,
        navigatable: true,
        editable: "incell",
        columns:[{
            field: "firstname",
        },{
            field: "surname",
        },{
            field: "classname",
        },{
            field
        : "age",
        }]

    });
like image 35
Samra Avatar answered Oct 22 '22 16:10

Samra