【问题标题】:How to use data from Model to bind as kendo datasource如何使用模型中的数据绑定为剑道数据源
【发布时间】:2017-05-25 02:56:12
【问题描述】:

我有一个空 div,我想使用模型中的数据将其初始化为剑道网格。它应该类似于以下内容,但我无法加载数据

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

关于.cshtml

@model List<KendoExample.Entities.ShortStudent>

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

我的Home Controller如下

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

我见过使用 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" }
    })
)
)

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

目前,我在屏幕上看到一个网格页脚,上面写着(4852 项中的 1 - 2 项),没有任何页眉或内容(数据行)。我做错了什么?

更新

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

现在带有标题的网格在那里但没有数据存在.. 如果我将操作更改为 json,它会在页面上返回纯 json

public ActionResult About()
    {
   ....
     return Json(students, JsonRequestBehavior.AllowGet);
 }

【问题讨论】:

  • 首先,您还没有向网格中添加任何字段。
  • 我现在已经按照建议添加了列,但现在只有页眉可用数据仍然不存在,可能它正在读取其他内容作为数据,因为页脚显示总共 5014 个项目

标签: asp.net-mvc kendo-grid


【解决方案1】:

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

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

});

【讨论】:

    【解决方案2】:

    我刚刚访问了 Telerik 的演示。尝试跟随。希望对你有所帮助,我的朋友。或者您可以访问此链接以参考更多信息: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"
    
        });
    

    【讨论】:

    • 我将控制器更改为返回 json(students),现在它向我显示页面上的 json,而不是任何 UI/网格
    • @Samra:您需要将 dataType: "odata" 更改为 "json"。访问此链接以参考更多,我的朋友:docs.telerik.com/kendo-ui/framework/datasource/overview
    • 嗨,Tomato,我确实从 odata 更改为 json,您可以在原始帖子中看到我的更新..我仍然卡住..感谢任何帮助,谢谢
    【解决方案3】:

    所以这就是我发现的应该直截了当的内容:)

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

    【讨论】:

    • 叹息,这正是我说你应该做的。
    猜你喜欢
    • 2013-08-08
    • 2015-01-19
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多