【问题标题】:How to reuse and parameter a Kendo UI grid EditorTemplate (using ASP MVC)?如何重用和参数化 Kendo UI 网格 EditorTemplate(使用 ASP MVC)?
【发布时间】:2015-05-22 06:38:02
【问题描述】:

我创建了一个 DropDownList EditorTemplate 用于网格内联编辑成功。现在我想在多个列(同一个网格)和/或具有不同网格的不同视图中重用这个模板。

到目前为止,我发现如果我在模板中省略下拉列表的“名称”,那么模板会自动绑定到网格中引用它的那个列(使用.EditorTemplateName(...))。然而,还有其他的东西应该首先参数化(显式或隐式)下拉数据源。

问:一个网格中有很多下拉菜单,如何参数化下拉数据源以防止复制和粘贴 DropDownListTemplate.cshtml 多次?

Q:在多列多视图中使用这个模板一般如何参数化?

观点:

@(Html.Kendo().Grid<Enumeration>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(e => e.Locale).Width(200)
            .EditorTemplateName("DropDownListTemplate");
        // columns.Bound(e => e.OtherColumn).Width(200)
        //    .EditorTemplateName("DropDownListTemplate", ???);

...和名为 DropDownListTemplate.cshtml 的模板放在 /Views/Shared/EditorTemplates 中

@model string
@(Html.Kendo()
  .DropDownListFor(m => m)
  .BindTo(ViewBag.LocaleDropDownListDataSource) // <- Having many dropdown in one grid, how to parameterize this, without copy and paste the DropDownListTemplate.cshtml zillon times?
  //.OptionLabel("Select Locale")
  .DataValueField("Locale")
  .DataTextField("Value")
  //.Name("Locale") // Omitting this binds the template automatically to the referring column in the grid. Using a custom .Name, what is not a column name in the grid ruins the working
  )

【问题讨论】:

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


    【解决方案1】:

    你为什么要重新发明轮子,剑道已经为我们提供了GridForeignKey 专栏,可以使用无数次。

    模板代码

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

    在网格中的实现

    columns.ForeignKey(col => col.DepartmentId, (IEnumerable) ViewBag.Departments, "Value", "Text").Title("Department");
    

    Demo

    【讨论】:

    • 是的,它不一定是 FK,您也可以将其称为“连接到您的收藏的属性”或“需要通过下拉列表选择的列”。不一定是ID,只要和集合值匹配就可以是文本字符串
    • 好的,很酷。对我来说唯一不清楚的是它指的是模板名称吗? (我想下拉模板位于“anyname.cshtml”中)
    • 当您设置此column.ForeignKey 时,它将引用GridForeignKey 模板。你可以在~/Views/Shared/EditorTemplates/GridForeignKey.cshtml找到它
    【解决方案2】:

    Dion 的回答确实是正确的,因为在外键(和类似)情况下,他解释了一个开箱即用的解决方案,所以我将其标记为答案。

    尽管如此,如何参数化和编辑器模板的一般问题仍然是一个实际问题,需要回答。构建器中的EditorViewData() 命名非常不言自明。 (好吧,在你找到它之后...... :-)

    观点:

    @(Html.Kendo().Grid<Enumeration>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(e => e.AnyColumn).Width(200)
                .EditorTemplateName("ReusableTemplate")
                .EditorViewData(new {AnyParameterName = anyValue1, OtherParameterName = otherValue1});
            columns.Bound(e => e.OtherColumn).Width(200)
                .EditorTemplateName("ReusableTemplate")
                .EditorViewData(new {AnyParameterName = anyValue2, OtherParameterName = otherValue2});
    

    ...以及名为 ReusableTemplate.cshtml 并放置在 /Views/Shared/EditorTemplates 中的模板

    @model object
    
    @{
        // Access the actual parameter values anywhere including the kendo helpers below (if any) via ViewData:
        var any = ViewData.AnyParameterName
        var other = ViewData.OtherParameterName
    }
    
    @(Html.Kendo()
      .AnyHelperYouWant_or_NoHelperAtAll
      )
    

    例如:

    @(Html.Kendo().Grid<Enumeration>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(e => e.AnyColumn).Width(200)
                .EditorTemplateName("ReusableTemplate")
                .EditorViewData(new {Name = "list1");
            columns.Bound(e => e.OtherColumn).Width(200)
                .EditorTemplateName("ReusableTemplate")
                .EditorViewData(new {Name = "list2"});
    

    并使用它:

    @model object
    
    @{
        // Access the actual parameter values:
        // Note: You can inline this variable if you want
        var name = ViewData.Name;
    }
    
    
    @(Html.Kendo().DropDownListFor(m => m).BindTo((SelectList)ViewData[name])
    

    【讨论】:

    • 在 EditorTemplate 中你还会使用 .BindTo 吗(比如说如果它是一个下拉菜单)?如果您提供了与上述问题有关的完整示例,我会投票赞成。
    • @Encription。是的,您仍然可以在任何合法的语法结构中使用。请参阅添加的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多