Null Reference
Null Reference

Reputation: 11360

How to create a kendoui grid programmatically

Assuming I have the following grid in my cshtml

@(Html.Kendo().Grid<Accessibility.Models.CompanyModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.Name);
        columns.Bound(c => c.Description);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Sortable()
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(p => p.Id))
        .Create(update => update.Action("Company", "Company"))
        .Read(read => read.Action("GetCompanies", "Company"))
        .Update(update => update.Action("Company", "Company"))
        .Destroy(update => update.Action("DestroyCompany", "Company"))
    )
)

How do I create the above grid programmatically instead of hard coding it in my cshtml file?

Upvotes: 0

Views: 884

Answers (1)

Mark Erasmus
Mark Erasmus

Reputation: 2405

Based on your comments, I would suggest creating a partial view containing the grid's markup, and then in the controller hydrate the model based on the user's input, and then return the partial view and the model. For example,

public ActionResult GetCompanies(int companyId)
{
    CompanyModel model = GetCompanies(companyId);

    return PartialView("_CompaniesGrid", model);
}

Upvotes: 1

Related Questions