I have a Kendo MVC Grid which is becoming really big and complex. I'd like to know how I can move some of the column definitions inside an helper method within the view. I can't figure out the right syntax for my helper method below.
@(Html.Kendo()
.Grid<MyModel>()
.Name("documents")
.Editable(e => e.Mode(GridEditMode.InLine))
.AutoBind(false)
.DataSource(b => b.Ajax()
.Model(m =>
{
m.Id(o => o.Id);
})
.Read(a => a.Action("documents", "document")
)
.Columns(c =>
{
c.Bound(m => m.Name)
.Title(Resources.Document.Name);
c.Bound(m => m.Description)
.Title(Resources.Document.Description);
...
if(ViewBag.CanViewEquipmentColumns) {
CreateEquipmentColumns(c); // The helper method
}
...
c.Bound(m => m.Size)
.Title(Resources.Document.Size);
})
)
// Here is the code that I can't get to work. How can I define this custom helper method here using the Razor view engine and Kendo Grid fluent API?
@{
Kendo.Mvc.UI.Fluent.GridBoundColumnBuilder<MyModel> CreateEquipmentColumns(Kendo.Mvc.UI.Fluent.GridColumnFactory<MyModel> c)
{
c.Bound(m => m.Equipment)
.Title(Resources.MaintenanceBooklet.Equipment);
c.Bound(m => m.StartDate)
.Title(Resources.MaintenanceBooklet.StartDate);
c.Bound(m => m.EndDate)
.Title(Resources.MaintenanceBooklet.EndDate);
}
}
答案 0 :(得分:0)
我不确定视图中的方法。你应该制作扩展方法,类似的东西:
public static void CreateEquipmentColumns<MyModel>(this GridColumnFactory<MyModel> factory, WebViewPage<YourViewModel> webViewPage)
{
factory.Bound(m => m.Equipment)
.Title(Resources.MaintenanceBooklet.Equipment);
}
你可以在视图中使用它:
Columns(c =>
{
c.Bound(m => m.Name)
.Title(Resources.Document.Name);
c.Bound(m => m.Description)
.Title(Resources.Document.Description);
...
if(ViewBag.CanViewEquipmentColumns) {
c.CreateEquipmentColumns(this); // The helper method
}
...
c.Bound(m => m.Size)
.Title(Resources.Document.Size);
})
答案 1 :(得分:0)
您可以像这样添加扩展方法,并且可以避免跨解决方案重复代码。
public static class KendoExtensions
{
public static GridBuilder<T> AddDefaultOptions<T>(this GridBuilder<T> builder) where T : mymodelClass // Your class here
{
return builder
.Columns(c =>
{
c.Bound(m => m.Equipment).Title(Resources.MaintenanceBooklet.Equipment);
c.Bound(m => m.StartDate).Title(Resources.MaintenanceBooklet.StartDate);
c.Bound(m => m.EndDate).Title(Resources.MaintenanceBooklet.EndDate);
});
}
}
查看强>
@(Html.Kendo().Grid(Model.Mydata)
.Name("mygrid")
.AddDefaultOptions()
)