【问题标题】:Telerik Kendo Grid Doesn't Display Value ProperlyTelerik Kendo Grid 无法正确显示值
【发布时间】:2019-09-25 02:45:53
【问题描述】:

我是剑道 UI 的新手,目前正在学习自定义编辑器。 我的问题是我设法让我的编辑器模板在编辑模式下工作并很好地填充数据,但不知何故它不会将值保存到显示网格

我从 API 中检索所有数据。

更新: 我已经设法将自定义编辑器模板中的值正确保存到控制器,它工作得很好,但是使用 clientTemplate 不会显示我在下拉列表中选择的正确值,而只会显示一个字符串

DropDown Only Display A String

我的设置代码是这样的

@( Html.Kendo().Grid<SalesOrderDetailVM>()
            .Name("list-detail")
            .Columns(columns =>
            {
                columns.Bound(c => c.Product).ClientTemplate("\\#=Product.ProductId\\#").Title("Products");
                columns.Bound(c => c.Quantity);
                columns.Bound(c => c.UnitPrice);
            })
            .Editable(GridEditMode.InCell)
            .ToolBar(tool =>
            {
                tool.Create();
                tool.Save();
            }
            )
            .DataSource(dataSource => dataSource
                .Ajax()
                .ServerOperation(false)
                .Batch(true)
                .Model(model =>
                {
                    model.Id(p => p.ProductId);
                    model.Field(p => p.Product);
                })
                .Create(act => act.Action("DetailCell_Create","SalesOrder"))
            )
)

DDLProduct.cshtml:

@model AAF.WEB.MVC.ViewModels.ProductVM

@(
        Html.Kendo().DropDownListFor(m => m)
                        .DataValueField("ProductId")
                        .DataTextField("ProductName")
                        .OptionLabel("Select Product")
                        .BindTo((System.Collections.IEnumerable)ViewData["products"])
)

Edit Mode
DisplayMode / Out of Product Edit Mode

【问题讨论】:

    标签: asp.net-mvc asp.net-core kendo-ui kendo-grid telerik-grid


    【解决方案1】:

    使用模板方法通过剑道网格实现下拉。

    GridForeignKey.cshtml - 它应该放在共享文件夹或 EditorTemplates @模型对象

    @(
     Html.Kendo().DropDownListFor(m => m)
            .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
    )
    

    请在您的剑道网格中进行如下更改

    @( Html.Kendo().Grid<AAF.WEB.MVC.ViewModels.SalesOrderDetailVM>()
                .Name("list-detail")
                .Columns(columns =>
                {
                    columns.Bound(c => c.Id)
                   columns.ForeignKey(c => c.ProductId, (System.Collections.IEnumerable)ViewData["Products"], "ProductId", "ProductName").Title("Products");
                    columns.Bound(c => c.Quantity);
                    columns.Bound(c => c.UnitPrice);
                })
                .Editable(GridEditMode.InCell)
                .ToolBar(tool =>
                {
                    tool.Create();
                    tool.Save();
                }
                )
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .ServerOperation(false)
                )
    )
    

    您可以设置产品数据查看数据。使用此方法您可以保存产品ID。

    谢谢

    【讨论】:

    • 使用外键确实有效,但它不会显示并将值保存到网格中。谢谢回复
    【解决方案2】:

    好吧,在我沮丧了几个小时之后,终于找到了解决方案

    解决方法是在网格中为传递的模型添加一个默认值

    .Model(model =>
                    {
                        model.Id(p => p.ProductId);
                        model.Field(p => p.Product).DefaultValue(
                                ViewData["defaultProduct"] as ProductVM
                            );
                    })
    

    并从控制器传递数据

    // Function that get data from API
    
    ViewData["products"] = products;
    ViewData["defaultProduct"] = products.First();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-27
      • 2020-01-09
      • 2017-11-10
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多