如何重用和参数化 Kendo UI 网格 EditorTemplate(使用 ASP MVC)?
How to reuse and parameter a Kendo UI grid EditorTemplate (using ASP MVC)?
我已经为网格内联编辑创建了一个 DropDownList EditorTemplate 成功。现在我想在多个列(相同网格)中重复使用此模板,and/or 在具有不同网格的不同视图中。
到目前为止我发现如果我在模板中省略下拉列表的 'Name' 那么模板会自动绑定到网格中引用它的那一列(使用 .EditorTemplateName(...)
) .然而,还有其他一些东西应该参数化(显式或隐式),首先是下拉数据源。
问:一个网格中有多个下拉列表,如何参数化下拉数据源以防止复制和粘贴DropDownListTemplate.cshtml zillon 次?
问:在多列、多视图中使用时,一般如何设置此模板的参数?
观点:
@(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
)
为什么要重新发明轮子,Kendo 已经为我们提供了 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");
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])
我已经为网格内联编辑创建了一个 DropDownList EditorTemplate 成功。现在我想在多个列(相同网格)中重复使用此模板,and/or 在具有不同网格的不同视图中。
到目前为止我发现如果我在模板中省略下拉列表的 'Name' 那么模板会自动绑定到网格中引用它的那一列(使用 .EditorTemplateName(...)
) .然而,还有其他一些东西应该参数化(显式或隐式),首先是下拉数据源。
问:一个网格中有多个下拉列表,如何参数化下拉数据源以防止复制和粘贴DropDownListTemplate.cshtml zillon 次?
问:在多列、多视图中使用时,一般如何设置此模板的参数?
观点:
@(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
)
为什么要重新发明轮子,Kendo 已经为我们提供了 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");
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])