Conditional check of DateTime in x-kendo-template<li>

5 posts, 0 answers
  1. Joe
    Joe avatar
    9 posts
    Member since:
    Jan 2016

    Posted 19 Jan Link to this post

    Approved is a DateTime in my model. approvedDate is a Datetime variable in my Razor view.

    I want to test Approved to see if it is before approvedDate and replace it with a empty string "" if it is inside the x-kendo-template detail-template.

     @{ var approvedDate = DateTime.Parse("01-01-2016"); }

     <script id="detail-template" type="text/x-kendo-template">

        <ul>

            < li>#if (# #= Approved # # < @approvedDate) {# ""#}else{# #= kendo.toString(Approved, "MM-dd-yyyy") # #}#</li>

         </ul>

    </script>

     The above syntax causes the detail-template script to fail.

    What is the syntax to make this if statement work?

     

     

     

  2. Petyo
    Admin
    Petyo avatar
    2439 posts

    Posted 21 Jan Link to this post

    Hello,

    please check this help article - it includes a througough description of how template works and has several examples, including if/else.

    Regards,
    Petyo
    Telerik
     
    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. Joe
    Joe avatar
    9 posts
    Member since:
    Jan 2016

    Posted 21 Jan in reply to Petyo Link to this post

    There were two problems, a syntax one and using a Razor DateTime variable in the conditional.

    I have both an if else and a ternary syntax working and have converted the Razor variable to a javascript Date.

    @{ var approvedDate = "01-01-2015"; }

    <script id="tab_template" type="text/kendo-tmpl">
    # var approvedDate = new Date(Date.parse(@approvedDate)); #

    ...

    <li><label>Test1: </label>#if (Applied < approvedDate ) {# #: kendo.toString(Applied, "MM-dd-yyyy") # #}else{# Fail #}#</li>
    <li><label>Test2: </label>#if (Applied > approvedDate ) {# #: kendo.toString(Applied, "MM-dd-yyyy") # #}else{# Fail #}#</li>
    <li><label>Test3:</label>#= Applied < approvedDate ? kendo.toString(Applied, "MM-dd-yyyy") : "Fail" #</li>
    <li><label>Test4:</label>#= Applied > approvedDate ? kendo.toString(Applied, "MM-dd-yyyy") : "Fail" #</li>

    ...

    </script>

    The following was displayed:

    Test1:  Fail           Test2:  08-22-2015            Test3: Fail             Test4: 08-22-2015

     Applied is a DateTime field in the model and its value is 08-22-2015.

  5. Petyo
    Admin
    Petyo avatar
    2439 posts

    Posted 25 Jan Link to this post

    Hi,

    most likely, the code generated by the server-side is not valid JavaScript. You may check that by using the web developer tools of your browser.

    Regards,
    Petyo
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. Joe
    Joe avatar
    9 posts
    Member since:
    Jan 2016

    Posted 25 Jan in reply to Petyo Link to this post

    You misunderstood my last post. 

    I said I had it working, explained the problems and showed the working code.

    The problem is resolved.

     

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