Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Larsen 116 posts 381 karma points
    Apr 13, 2013 @ 23:51
    Daniel Larsen
    0

    if else not working

    Hi, I am trying to make a daily menu widget thingy and it does output data, but I won't output the message, from my "else". I don't know if i am missing something, but a pair of fresh eys would be apreciated :-)

    @foreach (var item in Model.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date"))
    {
        if (item != null)
        {
            <h1>@item.Name</h1>
            <p>@item.lunchDish</p>
            <p>@item.lunchAcc</p>
        }
        else {  
            <p>No menu for today</p>
        }
    }
    

    Thanks :-)

    /Daniel

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 14, 2013 @ 19:04
    Jan Skovgaard
    0

    Hi Daniel

    Are you certain that you will get null returned? Perhaps you should check if item is empty as well?

    /Jan

  • Daniel Larsen 116 posts 381 karma points
    Apr 14, 2013 @ 20:11
    Daniel Larsen
    0

    Hmm, it does not look like it returns null. But how do I do it then? 

    Thansk you for the response :-)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 14, 2013 @ 20:25
    Jan Skovgaard
    0

    Hi Daniel

    Not sure if you can check on item alone actually but you should be able to check on @item.Name.

    I think you should be able to do this if(!String.IsNullOrEmpty(@item.Name)){yourstuff here}...

    Hopet this helps - otherwise I'm sure the Razor gurus in here will put me straight :-)

    /Jan

  • Daniel Larsen 116 posts 381 karma points
    Apr 15, 2013 @ 10:07
    Daniel Larsen
    0

    Thank you, but still no luck. It will not output the error message, when there is no menu for today. But at least your suggestion made perfect sense ;-)

  • Mike Chambers 636 posts 1253 karma points c-trib
    Apr 15, 2013 @ 10:18
    Mike Chambers
    0

    could be that it's returngin a DynamicNull?

     

    try writing out item.GetType()   to see what's occuring.

     

    @foreach(var item inModel.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date"))
    {
    @(item.GetType())
           
    if(item !=null)
           
    {
                   
    <h1>@item.Name</h1>
                    <p>@item.lunchDish</
    p>
                   
    <p>@item.lunchAcc</p>
            }
            else {  
                    <p>No menu for today</
    p>
           
    }
    }
  • Daniel Larsen 116 posts 381 karma points
    Apr 15, 2013 @ 10:24
    Daniel Larsen
    0

    It returns this: umbraco.MacroEngines.DynamicNode

    :-)

  • Charles Afford 1163 posts 1709 karma points
    Apr 15, 2013 @ 21:46
    Charles Afford
    0

    Hi is this all the code you have?  Can we see the rest of it.  Is there a red line underneath the @ besides @foreach??
    I am guessing you are using razor?  and everything exepct the else block is working.
    It think it is because you are never hitting null.  Do you get any other execptions?
    Your code seems a bit odd, esp using var item = .... instead of its proper type.  :)  Charlie :)
     

  • Daniel Larsen 116 posts 381 karma points
    Apr 16, 2013 @ 09:24
    Daniel Larsen
    0

    Yeah Charlie, I use Razor and yes, it is all of the code, except of this in the top: 

    @inherits umbraco.MacroEngines.DynamicNodeContext

    I think you are right about not hitting null. I don't get any more exceptions.

    How would you do it?

    I am not a razor guru, so any new knowledge is appreciated :-)

    Thanks :-)

     

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 09:56
    Charles Afford
    0

    Hi, if you dont have a custom model.  Then how are @item.lunchdish and @item.lunchAcc being rendered out correctly?. What version of Umbraco are you using?

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 10:01
    Charles Afford
    0

    What i would expect to see is something like:

     

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    {

    @foreach (var item in Model.Content.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date"))

    {

    <h1>item.Name</h1>

    <p> item.GetProperty("lunchDish").Value.ToString();lunchDish</p>

    <p> item.GetProperty("lunchAcc").Value.ToString();lunchDish</p>

    }

    }

     

    Hope this helps.  Charlie :)

  • Daniel Larsen 116 posts 381 karma points
    Apr 16, 2013 @ 10:40
    Daniel Larsen
    0

    I am using  v4.11.4. 

    It is outputting data correctly, when data is present. 

    I can't see how this should help me output a message, if there is no node today :-)

  • Mike Chambers 636 posts 1253 karma points c-trib
    Apr 16, 2013 @ 10:53
    Mike Chambers
    0

    Looked again and spotted your issue..

    @foreach(var item inModel.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date"))
    {
           
    if(item !=null)

    the foreach construct means that item will never be null... as you are stepping through the returned items ;-)

     

    you'd have to do something like this perhaps?

     

    @foreach(var item inModel.AncestorOrSelf("FrontPage").Descendants("LunchDay"))
    {
           
    if(@item.lunchDate != DateTime.Now.Date){
    NOT TODAY HERE
    }else {
    YOUR NORMAL CODE
    }

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 11:12
    Charles Afford
    0

    Mike you do have a good point there :') 

  • Daniel Larsen 116 posts 381 karma points
    Apr 16, 2013 @ 13:25
    Daniel Larsen
    0

    Now it sort of works. Problem now is, that it takes alle the following days too. I only need one menu a day :-)

    Todays menu is bla bla bla

    No menu for today.

    No menu for today.

    No menu for today.

    etc...

    I need to limit it to only take one... anyone? :-)

    But thank you, it got me closer to the goal :-)

  • Mike Chambers 636 posts 1253 karma points c-trib
    Apr 16, 2013 @ 13:38
    Mike Chambers
    1

    ok so you could do a check prior to the foreach

     

    var list = Model.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date");
    if (list.Count() > 0){
    @foreach(var item in list){
    }else{
    NOT TODAY
     

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 14:16
    Charles Afford
    0

    Can you do a take statement on the end of the where?  so Where("lunchDate == DateTime.Now.Date").Take(1)??;

    Also you are taking a single item but calling your var a list? and then going though a loop. This does not make sense.

    Charlie :)

  • Daniel Larsen 116 posts 381 karma points
    Apr 16, 2013 @ 14:44
    Daniel Larsen
    100

    Here is the result and it works!

    Note that I added the .Take(1) to the foreach.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{ var list = Model.AncestorOrSelf("FrontPage").Descendants("LunchDay").Where("lunchDate == DateTime.Now.Date");

    if (list.Count() > 0)
    {
    foreach (var item in list.Take(1))
    {
    <b>@item.Name</b>
    <p>@item.lunchDish</p>
    <p>@item.lunchAcc</p>
    }
    }

    else
    {
    <p>No menu for today</p>
    }

     Thank you all for the help. You have made this student very happy! :-)

    Daniel 

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 15:19
    Charles Afford
    0

    Hi Daniel, what is this for?.  Although it works i think you could work the logic a bit better :).  Charlie.

  • Daniel Larsen 116 posts 381 karma points
    Apr 16, 2013 @ 15:25
    Daniel Larsen
    0

    Hi Charlie. It is a way to show the daily menu, but only the relevant one, the menu of the day, so no need to show the others. 

    What do you mean with the logic? How would you do it? :-)

    Daniel 

  • Charles Afford 1163 posts 1709 karma points
    Apr 16, 2013 @ 22:39
    Charles Afford
    0

    Sorry have not had time to look this evening.  You could either create a method in a class libary that returns a list of menus.  Complie the dll into project and then pass your NodeId into that method and get a list back.  Then in your razor code you would do something with a list.

    I think its really obsure that you are using DynamicNodeContext. It works which is the main thing :D.

  • Daniel Larsen 116 posts 381 karma points
    Apr 17, 2013 @ 10:56
    Daniel Larsen
    0

    What is wrong using the DynamicNodeContext? 

    I am trying to keep it as simple as possible. If I can keep everything in razor macros, without usercontrols and such. I am not a programmer :-)

     

Please Sign in or register to post replies

Write your reply to:

Draft