Getting a 'null' value in client template of Kendo ListView

4 posts, 0 answers
  1. Amorphous2010
    Amorphous2010 avatar
    4 posts
    Member since:
    Aug 2009

    Posted 20 Aug 2013 Link to this post

    I have a simple Kendo ListView with an applied client template. If the data in the row item to which the template is applied happens to be null, then the text 'null' appears by default. How can I change this behavior without having to do ${ ShipToName == null ? '' : ShipToName } every time? I am using a template that has to display numerous bound data items, and having to check for null on each and every one of them is tedious and results in code bloat. Even more importantly (and possibly a second question), I need to be able to evaluate the data value from within one of the Razor MVC HTML wrappers, and am constantly getting an 'Invalid template' exception. Please help!

    Example #1:

    <script type="text/x-kendo-tmpl" id="editTemplate">
     //This is always 'null' by default
     //This correctly displays null values as empty string
     ${ ShipToName == null ? '' : ShipToName }

    Example #2:

    <script type="text/x-kendo-tmpl" id="editTemplate">
    //This works, but always displays 'null' as the value in the textbox
    @Html.TextBox("ShipToName", "#=ShipToName#")
    //This causes an 'Invalid template' error
    @Html.TextBox("ShipToName", "#if (ShipToName != null) {# #=''# #} else {# #=ShipToName# #}#")

  2. Petur Subev
    Petur Subev avatar
    1882 posts

    Posted 22 Aug 2013 Link to this post

    Hello Shawn,

    I am afraid that there is not other way - you need to check if the value is null, however you can use an external function ti save some space.

    Regarding the second question:

    @Html.TextBox("ProductName", "#if (ProductName != null) {# #=''# #} else {# #=ProductName# #}#")

    The second parameter (the value) becomes encoded which ends as an invalid template. Use regular input element instead.

    Petur Subev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Matt
    Matt avatar
    5 posts
    Member since:
    Jun 2014

    Posted 10 Aug 2014 in reply to Petur Subev Link to this post

    You probably mean: ProductName == null
    Then it shows the product name if it is not null.

    @Html.TextBox("ProductName", "#if (ProductName == null) {# #=''# #} else {# #=ProductName# #}#")
  4. bobthecoder
    bobthecoder avatar
    4 posts
    Member since:
    Sep 2014

    Posted 06 Jul 2015 in reply to Petur Subev Link to this post

    you have ProductName != null above. I think you mean ProductName == null
Back to Top