This is a migrated thread and some comments may be shown as answers.

Kendo Grid ClientTemplate calling for a custom helper

3 Answers 540 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 18 Oct 2013, 01:22 AM
I have tried several things with no success. The most I can is the helper to be called BUT instead of the value being passed it passes #: Style # as string instead of its value

without calling the helper i can display all values

My question is: how do I call a helper that has parameters inside a ClientTemplate?


Helper (MyHelpers.cshtml on app_code folder)

@helper ProductDisplay(string Style, string DefaultColor, string Name)
{
    <div class="prodName" data-id="@(Style + "-" + DefaultColor)">@Name</div>
    string img = "http://url/image/" + Style + DefaultColor + "_1?$ereredq$&bregc=0,0,0,0";
    <text><img src="@img" /></text>
}


Grid
============================================        
@(Html.Kendo().Grid<TestProject.Models.ProductModel.ProductInformation>()
        .Name("webGrid").AutoBind(true).Scrollable(scr=>scr.Height(430))
        .DataSource(dataSource => dataSource 
            .Ajax()
            .Read(read => read.Action("GetProducts", "Home")
           )
            .ServerOperation(false)
        )
        .Columns(columns =>
        {
            columns.Template(@<text></text>).ClientTemplate(@MyHelpers.ProductDetail("#: Style #, #: DefaultColor #, #: Name #").ToHtmlString());
        })
              .Pageable()
              .Scrollable()
   )

3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 22 Oct 2013, 06:54 AM
Hello Carlos,

The behavior is expected as those binding expressions (#:Style# etc.) are evaluated on client when the Grid is populated with data, while you are using them as string parameters. Here is how the correct template helper should looks like. Notice that the template expressions are inlined in the helper instead as arguments.

/the template helper/

@helper ProductDisplayTemplate()
{
    <div class="prodName" data-id="#:Style#-#:DefaultColor#">#:Name#</div>   
    <img src="http://url/image/#:Style##:DefaultColor#_1?$ereredq$&bregc=0,0,0,0" />
}

/column definition/
.Columns(columns =>
{
 columns.Template(@<text></text>)
   .ClientTemplate(ProductDisplayTemplate().ToHtmlString());
}

Regards,
Nikolay Rusev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Carlos
Top achievements
Rank 1
answered on 22 Oct 2013, 08:16 PM
Thanks. It worked.

The second issue is that one of the helper contains a call to a method and uses its data. I changed this one but did not work. Is it possible to work on ClientTemplate ??

Part of the current one is (prior to your suggested change):
@helper StyleGrid(string Style)
    {
        UserSession User = new UserSession();
        ProductModel.ColorResults Colors = ProductModel.GetStyleColors(Style, User.Customer.CustomerNumber);
         
    <div class="grid">
    <div class="colorContainer">
    <div class="CartColor"> </div><div class="DealerPrice"><b>Retail</b></div>
 
        <div class="DealerPrice"><b>Dealer</b></div>
    @if (Colors.Colors != null)
    {
    foreach (ProductModel.ColorResults.Color color in Colors.Colors)
if (color.Sizes != null)
        {
            <div class="colorRow">
 
etc
0
Nikolay Rusev
Telerik team
answered on 23 Oct 2013, 06:36 AM
Hello Carlos,

The helper in question will not be suitable for client template for the same reason. The data is available on the client, not when the template is defined and it will be evaluated on client when there isn't server context available.

It should be reworked in the same manner as in the first post. 

Regards,
Nikolay Rusev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Carlos
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Carlos
Top achievements
Rank 1
Share this question
or