如何使用Model中的数据绑定为kendo数据源

Sam*_*mra 9 asp.net-mvc kendo-grid

我有一个空的div,我想使用来自Model..it的数据初始化为一个kendo网格应该是类似以下但我无法加载数据

$("#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"
});
Run Code Online (Sandbox Code Playgroud)

About.cshtml

@model List<KendoExample.Entities.ShortStudent>

<div class="row">
<div class="col-md-12 table-responsive" id="mapsDiv">        
</div>
Run Code Online (Sandbox Code Playgroud)

我的家庭控制器如下

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);
Run Code Online (Sandbox Code Playgroud)

我见过使用json而不是odata的例子......

此外,还有一些例子可以使用它

@(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" }
    })
)
)
Run Code Online (Sandbox Code Playgroud)

我无法理解/实施,所以请忽略这种解决方案.

目前我看到一个网格页脚,显示(4852项中的1-2个),屏幕上没有任何标题或内容(数据行).我究竟做错了什么?

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",
        }]
    });
Run Code Online (Sandbox Code Playgroud)

HomeController的

 public ActionResult About()
    {
   ....
     return View(students);
 }
Run Code Online (Sandbox Code Playgroud)

现在带有标题的网格存在但没有数据存在.如果我将动作更改为json,它将在页面上返回普通的json

public ActionResult About()
    {
   ....
     return Json(students, JsonRequestBehavior.AllowGet);
 }
Run Code Online (Sandbox Code Playgroud)

Kri*_*ris 5

您是否尝试将字段添加到网格中?

$("#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"

});
Run Code Online (Sandbox Code Playgroud)


Sam*_*mra 1

所以这就是我发现的应该是直接的:)

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",
        }]

    });
Run Code Online (Sandbox Code Playgroud)