I would like to create a custom Html helper for a Kendo grid. We have a particular view-model combination which will be used across multiple controllers. I would like to be able to create a custom help method for display a grid of these models. It would basically be a short cut to use in all the views so we wouldn't have to maintain the same code in many different files. I would like to call it like this:
And have it output all the bells and whistles of a full grid declaration, like this:
@(Html.Kendo().Grid(@Model)
.Name(
"KnockoutQuestions"
)
.DataSource(datasource => datasource
.Ajax()
.Model(model =>
{
model.Id(k => k.Id);
model.Field(k => k.Id).Editable(
false
);
model.Field(k => k.Enabled).DefaultValue(
true
);
})
.Events(events => events.Error(
"Error"
))
.Create(create => create.Action(
"Create"
,
"KnockOutQuestion"
))
.Read(read => read.Action(
"Read"
,
"KnockOutQuestion"
))
.Update(update => update.Action(
"Update"
,
"KnockOutQuestion"
))
)
.Columns(columns => {
columns.ForeignKey(k => k.AdminLeadType,
new
SelectList(from pair
in
BaseKnockOutQuestionModel.EnumLeadTypes select
new
{ text = pair.Value, value = pair.Key },
"value"
,
"text"
));
columns.ForeignKey(k => k.QuestionTypeId,
new
SelectList(from pair
in
BaseKnockOutQuestionModel.QuestionTypes select
new
{ text = pair.Value, value = pair.Key },
"value"
,
"text"
));
columns.Bound(k => k.QuestionText);
columns.Bound(k => k.Answer1Text);
columns.Bound(k => k.Answer2Text);
columns.Bound(k => k.Price);
columns.Bound(k => k.Enabled);
columns.Command(command => command.Edit());
})
.ToolBar(toolbar => { toolbar.Create(); })
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Pageable()
.Sortable()
.Filterable()
)
I know how to do custom Html helper methods, but I haven't found any examples of how to do a shortcut like this. It's probably something simple like using the "this HtmlHelper helper" param to execute and return the output of the Kendo().Grid() function. I wanted to ask anyways so I could hopefully avoid common mistakes/pitfalls.