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

How to pass variable to custom helper in clientTemplate

8 Answers 433 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jerry
Top achievements
Rank 1
Jerry asked on 23 Jan 2014, 09:45 PM
Hi there,

I have a problem, I want to set cell background programmatically. Is there any way to pass variable to a customer helper in ClientTemplate?


grid
columns.Bound(c => c.RiskProfileLevel).ClientTemplate(RiskProfileLevelTemplate(/*RiskProfileLevel*/).ToHtmlString());

Helper
@helper RiskProfileLevelTemplate(int riskProfileLevel)
{
        if (riskProfileLevel == 1)
        {
            <div style="background-color: @CorporateSys.Website.Models.SiteUtil.RED_STYLE; width: 100%; height: 33px; margin: 0; padding: 0;"></div>
        }
        else if (riskProfileLevel == 2)
        {
            <div style="background-color: @CorporateSys.Website.Models.SiteUtil.AMBER_STYLE; width: 100%; height: 33px; margin: 0; padding: 0; "></div>
        }
        else if (riskProfileLevel == 3)
        {
            <div style="background-color: @CorporateSys.Website.Models.SiteUtil.GREEN_STYLE; width: 100%; height: 33px; margin: 0; padding: 0; "></div>
}

Thanks in advance


regards
Jerry

8 Answers, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
answered on 23 Jan 2014, 11:10 PM
Hey Jerry, I don't think there would be any problems with that.

<!-- codeblock outside of grid -->
@{
    string bar = "bar";
}
 
<!-- codeblock inside of grid.Columns -->
columns.Bound(report => report.ReportType).Title("Report Type").ClientTemplate(string.Format("foo {0}", bar))


This utilizes bar as a parameter to string.Format, and all my Report Type column says is "foo bar", so it seems to accept variables no problem.

Is it not working for you?
0
Jerry
Top achievements
Rank 1
answered on 24 Jan 2014, 12:22 AM
Hi Andrew,

Thank you for your reply.

That's not what I want to be honest. Maybe I didn't express myself properly.

RiskProfileLevel is a property of the data model that the grid bind to. I want to pass that property to the helper method, thus display different background depend on the value of RiskProfileLevel.

Any idea?


regards
Jerry
0
Andrew
Top achievements
Rank 1
answered on 24 Jan 2014, 04:17 PM
Oh I think I know what you mean.
Normally in a
foreach(item in Model.SomeCollection)
{
    *some html*
    RiskProfileLevelTemplate(item.riskProfileLevel).ToHtmlString()
    *some html*
}
we get access to each item in some collection cause we're looping through them, but we don't have that to work with inside the templates, we get
#= riskProfileLevel #
instead.

I run into this problem when trying to use variables inside of default Html helpers as well. It renders the inner Html helper, then it renders the Kendo helper, so it passes in "#= riskProfileLevel #" as a string parameter, which isn't useful.

If riskProfileLevel is not a property of a collection item, and is just a member of Model where Model is not a collection, then you could just pass in Model.riskProfileLevel without any problems.

Otherwise you're in my boat. :\
0
Accepted
Alexander Popov
Telerik team
answered on 27 Jan 2014, 01:34 PM
Hello Jerry,

You could achieve the same behavior using Kendo UI Template, as seen in this screenshot.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jerry
Top achievements
Rank 1
answered on 27 Jan 2014, 08:36 PM
Hi Alexander,


Thank you for your reply. That works like a charm.

So, is it not possible to do it by using the @helper way described in my first post?

regards
Jerry
0
Alexander Popov
Telerik team
answered on 29 Jan 2014, 11:44 AM
Hi Jerry,

This could be achieved using helpers, however there are no real benefits. Basically the helper is executed on the server, while the client templates are executed on the client, thus the helper should return a valid Kendo UI Template.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jerry
Top achievements
Rank 1
answered on 29 Jan 2014, 08:31 PM
Hi Alexander,


Just thought it might make the code more clean. Could you tell me how to achieve this by using helper?

Thank you

regards
Jerry
0
Alexander Popov
Telerik team
answered on 31 Jan 2014, 01:05 PM
Hello Jerry,

I am afraid that custom MVC helpers are beyond the scope of our standard support services, however information on this topic is available on various resources over the Internet.

Regards,
Alexander Popov
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
Jerry
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Jerry
Top achievements
Rank 1
Alexander Popov
Telerik team
Share this question
or