Use different datasource with same Grid as partial view

2 posts, 0 answers
  1. Peng
    Peng avatar
    13 posts
    Member since:
    Aug 2010

    Posted 01 Feb Link to this post

    Hi There,

     

    I have a situation where I will have to do many same pages with the same Grid columns and functionality, but it will have a bit different dataSource I will have to read from the backend controller. Is there a way I can template the Grid itself and reuse it but be able to switch the dataSource?

     

    For example.

     

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CustomerViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.ContactName).ClientTemplate(
                    @"<div class='customer-photo'
                        style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>
                    <div class='customer-name'>#: ContactName #</div>")
              .Width(240);
            columns.Bound(c => c.ContactTitle);
            columns.Bound(c => c.CompanyName);
            columns.Bound(c => c.Country).Width(150);
        })
        .HtmlAttributes(new { style = "height: 550px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Customers_Read", "Grid"))
            .PageSize(20)
        )
    )

     

     

    I want the

     

    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Customers_Read", "Grid"))
        .PageSize(20)
    )

     

    Customers_Read to be switched to OldCustomers_Read

    or NewCustomers_Read

    or CustomersEtc_Read

    But I don't want to copy everything of the Grid over and over. 

    How can I do this?

     

    Thank You

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2053 posts

    Posted 03 Feb Link to this post

    Hi Peng,

    You could try something like the following:
    .Read(read => read.Action(ViewData["ReadAction"].ToString(), "Grid"))

    Hope this helps.


    Best Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top