【问题标题】:How do I use the model as a datasource for Kendo UI grid?如何将模型用作 Kendo UI 网格的数据源?
【发布时间】:2019-10-31 08:54:22
【问题描述】:

我只得到一个内部没有数据的网格。我是 MVC 的新手,并且无法找到如何仅使用传递给视图页面的模型将数据添加到网格中。下面是我用过的代码

控制器代码:

  public ActionResult List()
    {
        List<CustomerModel> list = new List<CustomerModel>();
        for (int i = 0; i < 7; i++)
        {
            CustomerModel model = new CustomerModel();
            model.Id = 1;
            model.Name = "Alice";
            model.Address = "Blk "+i;
            model.PostalCode = ""+i+i+i;
            model.Email = i+"@yahoo.com";
            model.TelephoneNumber = i + i + i + i + i + i+"";
            list.Add(model);
        }


        return View(list);
    }
    public ActionResult Read()
    {
        List<CustomerModel> list = new List<CustomerModel>();
        for (int i = 0; i < 7; i++)
        {
            CustomerModel model = new CustomerModel();
            model.Id = 1;
            model.Name = "Alice";
            model.Address = "Blk " + i;
            model.PostalCode = "" + i + i + i;
            model.Email = i + "@yahoo.com";
            model.TelephoneNumber = i + i + i + i + i + i + "";
            list.Add(model);
        }


        return View(list);
    }

查看代码:

@using TKMVC.Models
@model IEnumerable<CustomerModel>


@(Html.Kendo().Grid<CustomerModel>()
    .Name("grid")
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(true)
        .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.Id).Editable(false);
        })
        .Read(read => read.Action("Read", "Home"))
    )
)
<div class="clearfix"></div>

【问题讨论】:

    标签: asp.net-mvc view model telerik kendo-grid


    【解决方案1】:

    您正在混合本地和 ajax 数据绑定,并且尚未在网格中定义任何列。

    尝试将模型传递到网格中,定义网格列并从数据源中删除读取请求。在您的情况下,您很可能需要关闭服务器操作。

    您的网格语句应如下所示:

    @(Html.Kendo().Grid<CustomerModel>(Model)
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(m => m.Id);
            columns.Bound(m => m.Name);
        })
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(false)
            .Model(model =>
            {
                model.Id(p => p.Id);
                model.Field(p => p.Id).Editable(false);
            })
        )
    )
    

    【讨论】:

    • 我在 ".DataSource(dataSource => dataSource" 的行中收到 "Cannot convert lambda expression to type 'string' because it is not a delegate type" 这是什么意思?
    • 不知道。这应该有效。该语句中唯一的字符串是网格名称。
    • 与 Telerik 示例的唯一区别是模型定义。如果这确实是问题所在,那可能是一个错误,值得向 Telerik 报告。
    猜你喜欢
    • 2016-08-25
    • 2014-03-08
    • 2013-07-15
    • 1970-01-01
    • 2014-02-10
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多