if/else Syntax within foreach - Code Showing Up in Display
Hi,
Can anyone tell me what is wrong with this code? Engine is making the comparison successfully but the if/else code (eg., if (Model.AncestorOrSelf(1).language == "English") {) is actually showing up in the display:
try { var t = new uTweets.Tweets(); var result = t.GetTweetsAbout(hashTag); if (result != null) { <div id="tweets"> <h6> Latest Tweets </h6> @foreach(var tw in result.Take(nrOfTweets)) { <div class="sidebar-content"> <p> if (Model.AncestorOrSelf(1).language == "English") { @tw.CreatedDate.ToString("MMM dd, yyyy @ h:mm tt") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime) } else { @tw.CreatedDate.ToString("dd MMM, yyyy @ HH:mm") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime) } </p> </div> } </div> } } catch (Exception ex) { @ex.ToString(); } <div style="clear:both"></div> }
I am new to Razor and I am sure this is something very basic but I am pulling my hair out on this one. I thought it was that I needed a "@" in front of the if but then I get an error when I try to save it. Any help would be greatly appreciated.
This was it! it was the hyphens -- and also the parentheses around the RelativeTime call. Wrapping these items intags solved the problem. Thanks very much Andy! High five!
if/else Syntax within foreach - Code Showing Up in Display
Hi,
Can anyone tell me what is wrong with this code? Engine is making the comparison successfully but the if/else code (eg., if (Model.AncestorOrSelf(1).language == "English") {) is actually showing up in the display:
I am new to Razor and I am sure this is something very basic but I am pulling my hair out on this one. I thought it was that I needed a "@" in front of the if but then I get an error when I try to save it. Any help would be greatly appreciated.
Thanks in advance,
Garrett
Hi Garrett,
Try this:
/Dennis
I'm afraid that doesn't work either. It looks like you just replaced the if with @if and as I said, I get an error trying to save the file:
Error occured
c:\...\macroScripts\635120935035700000_uTweets.TweetsWithHashTag.cshtml(21): error CS1002: ; expected
This one's a real mystery. Any other ideas?
Thanks again,
Garrett
I think it's those hypens - Razor is treating them as code and then expecting to see a ; at the end.
Try surrounding them with <text></text> to make in unambiguous, i.e.
And you will need the @ before the if too.
Hope that sorts it for you.
Andy
This was it! it was the hyphens -- and also the parentheses around the RelativeTime call. Wrapping these items intags solved the problem. Thanks very much Andy! High five!
//Garrett
is working on a reply...