Conditionals embedded in client templates

4 posts, 0 answers
  1. Robert
    Robert avatar
    23 posts
    Member since:
    Feb 2009

    Posted 03 Apr 2013 Link to this post

    I have model class like so:
    public class ServerModel
    {
        public Guid Id { get; set; }
        public CustomerModel Customer { get; set; }
        public string Name { get; set; }
    }

    Inside a Kendo Grid, I'd like to display a link to the Customer, but as the Customer can be null, I'm having trouble getting this link to only show when the customer is not null. Below is the client template I'm trying to use:
    @(Html.Kendo().Grid(Model)
          .Name("Grid")
          .Columns(columns =>
          {
              columns.Bound(s => s.Name)
                     .ClientTemplate("<a href='" + Url.Action("Details", "Server") + "/#= ID #'>#= Name #</a>");
              columns.Bound(s => s.Hostname);
              columns.Bound(s => s.Customer.Name).Title("Customer")
                     .ClientTemplate("#= Customer ? '<a href=\'" + Url.Action("Details", "Customer") + "/#= ID #\'>#= Customer.Name #</a>' : 'Unknown'#");
              columns.Bound(s => s.Reported).Title("Last Reported (UTC)").Format("{0:yyyy-MM-dd hh:mm:ss tt}");
          })

    This just gives me a template error, though.
  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2153 posts

    Posted 04 Apr 2013 Link to this post

    Hello Robert,


    To include the check for the null value in your ClientTemplate, you could modify it like this
    E.g.
    columns.Bound(p => p.ProductName).ClientTemplate("#if(Name != null){#" +
               "<a href='" + Url.Action("Details", "Server") + "/#=ID#'>#=Name#</a>" +
               "#} else {#" +
               "another template" +
               "# } #");

     

    Greetings,
    Dimiter Madjarov
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Nathan
    Nathan avatar
    23 posts
    Member since:
    Jul 2012

    Posted 07 Apr 2014 in reply to Dimiter Madjarov Link to this post

    How would I attempt to do this if I'm building the grid on the server (using Razor) and doing a footer template?  Sometimes the values may be null.  So if it's null, I want to add a 0 or string.empty

    columns.Bound(c => c.Flowtime).Title("Time").Format(numericalformat).FooterTemplate(@<text>@item.Sum.Format(numericalformat)</text>);
    .....
    agg.Add(p => p.Flowtime).Sum();
  5. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2153 posts

    Posted 08 Apr 2014 Link to this post

    Hi Nathan,

    I see that my colleague Alexander covered the issue in the support ticket on the same topic. Do not hesitate to contact us again if further problems arise.


    Regards,
    Dimiter Madjarov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready