【问题标题】:ForeignKey column does not select value in Kendo-GridForeignKey 列不选择 Kendo-Grid 中的值
【发布时间】:2013-02-05 16:05:45
【问题描述】:

我有一个包含一些列的网格,其中一个列是 foreignKey 列。

我想在组合框中显示该列的所有可能值。 ViewData["States"] 是一个 IList<State>,其中 State 有一个 Id 字段和一个 Name 字段。

为此,我修改了模板“GridForeignKey.cshtml”,如下所示:

@model object

@(
 Html.Kendo().ComboBoxFor(m => m)        
    .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") +  
   "_Data"]).Filter(FilterType.Contains).Placeholder("Select...")
)

我的视图如下所示:

<div class="contentwrapper">
    @{
        ViewBag.Title = "Home Page";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    @(
    Html.Kendo().Grid<CustomerModel>()    
    .Name("Grid")
    .Columns(columns => {
        columns.Bound(p => p.Name);

        columns.ForeignKey(p => p.StateId, (IEnumerable)ViewData["States"], "Id", "Name");

        columns.Bound(p => p.StreetAddress).Width(140);
        columns.Bound(p => p.Zip).EditorTemplateName("Integer");
        columns.Command(command => { command.Edit(); command.Destroy(); });//edit and delete buttons
    })
    .ToolBar(toolbar => toolbar.Create())//add button
    .Editable(editable => editable.Mode(GridEditMode.InLine))//inline edit
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource        
        .Ajax()                 
        .Events(events => events.Error("error_handler"))
        .Model(model => {
            model.Id(c => c.CustomerId);
            model.Field(c => c.CustomerId).Editable(false);
            model.Field(c => c.Zip).DefaultValue(4444);
        }
        )//id of customer
        .Create(update => update.Action("EditingInline_Create", "Customer"))
        .Read(read => read.Action("EditingInline_Read", "Customer"))
        .Update(update => update.Action("EditingInline_Update", "Customer"))
        .Destroy(update => update.Action("EditingInline_Destroy", "Customer"))
    )
)
</div>
<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.e`enter code here`ach(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);//show error
        }
    }
    </script>

现在我的问题:

  1. 我的表格未显示“状态”的选定值。
  2. 当我编辑一行时,组合框出现并且其中包含所有所需的值,但未选择该值。相反,它总是显示占位符文本。
  3. 在我将一个复杂对象绑定到我的网格之前,该对象有一个本身就是复杂类型的字段(State 包含IdName 属性)但不知何故剑道不允许我像@987654330 那样绑定它@。这就是我扁平化模型的原因,现在我使用字段StateId 代替。甚至可以使用这样的级联复杂类型吗?

【问题讨论】:

    标签: grid kendo-ui


    【解决方案1】:

    你所拥有的将不起作用。您需要将 EditorViewData 中的列表传递给 EditorTemplate,并告诉它要使用哪个 EditorTemplateName。

    columns.ForeignKey(p => p.StateId, (IEnumerable)ViewData["States"], "Id", "Name")
        .EditorViewData(new { statesList = ViewData["States"] })
        .EditorTemplateName("GridForeignKey");
    

    和GridForeignKey.cshtml一样

    @model int // Assuming Id is an int
    @(Html.Kendo().ComboBoxFor(m => m)
        .Placeholder("Select...")
        .DataValueField("Id")
        .DataTextField("Name")
        .BindTo(ViewData["statesList"])
    )
    

    这可能并不完全正确,因为我只是在脑海中做到了。但它至少应该让你朝着正确的方向前进。

    【讨论】:

    • 我有点困惑:您似乎将视图数据传递了两次?!当我选择外键列时,隐式调用了 GridForeignKey-Template。无论如何,我尝试了您的建议,结果与我的结果完全相同。 (问题仍然存在)。但仍然感谢您的回复!
    【解决方案2】:

    要实现你想要的,你需要在网格中定义编辑事件后实现一些客户端脚本

     .Events(events => events.Edit("onEdit"))
    
    if your grid called **myGrid** then your script will be in this way
    
    <script>
    $(document).ready(function (e) {
        var innerContent = $(".k-grid-delete").html().replace("Delete", "");
        $(".k-grid-delete").html(innerContent);
    });
    
    function onEdit(e) {
        $("#**myGrid** tbody [data-role=dropdownlist]").each(function () {
            var ddl = $(this).data("kendoDropDownList");
            if (ddl) {
                ddl.options.optionLabel = "Select";
                ddl.refresh();
                ddl.value("");
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 2014-01-24
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 2021-11-18
      相关资源
      最近更新 更多