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>
}
}
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 ;-)
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 :)
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?
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 } }
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.
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 :-)
Thanks :-)
/Daniel
Hi Daniel
Are you certain that you will get null returned? Perhaps you should check if item is empty as well?
/Jan
Hmm, it does not look like it returns null. But how do I do it then?
Thansk you for the response :-)
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
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 ;-)
could be that it's returngin a DynamicNull?
try writing out item.GetType() to see what's occuring.
It returns this: umbraco.MacroEngines.DynamicNode
:-)
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 :-)
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?
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 :)
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 :-)
Looked again and spotted your issue..
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?
Mike you do have a good point there :')
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 :-)
ok so you could do a check prior to the foreach
if (list.Count() > 0){
}else{
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 :)
Here is the result and it works!
Note that I added the .Take(1) to the foreach.
Thank you all for the help. You have made this student very happy! :-)
Daniel
Hi Daniel, what is this for?. Although it works i think you could work the logic a bit better :). Charlie.
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
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.
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 :-)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.