I'm new to Razor and trying to carry out what should be a simple task but my code is not working.
I am displaying the results of a query (in a partial) and within that query I have set up a counter so that I can wrap different HTML around the items, depending which number they are.
So, it looks like this:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{ var selection = CurrentPage.Site().FirstChild("articleRepo").Children("article").Where("Visible").OrderBy("CreateDate desc").Take(10); }
@if (selection.Any())
{
var n = 0;
<div>
@foreach (var item in selection)
{
var imagePath = @item.image640;
n++;
if(n==1){
do one things
} else {
do another
}
<div class="col-sm-6 hp-two-story">
@n<img class="lazy" data-original="@("http://www.website.org" + imagePath)" />
<p><a href="@item.Url">@item.Name</a></p>
</div>
}
</div>
}
The content shows just fine until I put the..
if(n==1){
do one things
} else {
do another
}
Then it throws errors. I msut have tried 100 variations of this, but I'm close to going insane. I get the same errors whether using Back OFfice or VS.
Yes, it can be a bit confusing when your new to Razor with before/after HTML tags. Generally if you are inside a HTML tag, it sholud be a @ before the statement, and if its inside another statment (if statement inside an foreach) then it should not have a starting @.
Razor if statement counter issue v7.4
Hi,
I'm new to Razor and trying to carry out what should be a simple task but my code is not working. I am displaying the results of a query (in a partial) and within that query I have set up a counter so that I can wrap different HTML around the items, depending which number they are.
So, it looks like this:
The content shows just fine until I put the..
Then it throws errors. I msut have tried 100 variations of this, but I'm close to going insane. I get the same errors whether using Back OFfice or VS.
Howver, if I do this:
I don;t get any errors.
The Razor is fussy about being before/after HTML tags?
I don't get it.
Yes, it can be a bit confusing when your new to Razor with before/after HTML tags. Generally if you are inside a HTML tag, it sholud be a @ before the statement, and if its inside another statment (if statement inside an foreach) then it should not have a starting @.
are you asking about outputting formatted text inside the if...else statement?
You will probably either want to use the <text>block</text> or @Html.Raw("output string") depending on your needs.
OK I got it to work using this code:
So is this a standard/good practice way to do it or have I just got lucky?
is working on a reply...