I am trying to add a class to my images depending on whether a checkbox is checked in the back office although I'm having some issues using the variables that I have defined.
@foreach (var item in newsItems)
{
// Display the pageTitle entered by the user if it exists otherwise just use the generic page name
var title = string.IsNullOrWhiteSpace(item.pageTitle)
? item.Name
: item.pageTitle;
var containerClass = item.IsLast("padding-bottom", "border-bottom");
if(item.relatedImageTransparent == true){
var imageClass = "backgroundFraming";
}else{
var imageClass = string.Empty;
}
<div class="@containerClass">
<section>
<h2><a href="@item.Url" class="blogLink">@title</a></h2>
<span class="glyphicon glyphicon-time"> </span><span class="timestamp">@item.CreateDate.ToString("f")</span>
<div class="row">
<div class="col-md-4 imageContent">
<img src="@Umbraco.Media(item.relatedImage).umbracoFile" class="img-responsive @imageClass" alt="Responsive Image">
</div>
<div class="col-md-7 textContent">
@if (string.IsNullOrWhiteSpace(item.subheader) == false)
{
<h3>@item.subheader</h3>
}
@Umbraco.Truncate(item.bodyText, 200)
<a href="@item.Url" class="btn btn-primary">Continue Reading</a>
</div>
</div>
</section>
</div>
}
The imageClass variable in my code above should apply an additional class to the image if the checkbox is checked in the back office. Otherwise it will append an empty string. However, when compiling and running my code I receive the following error:
Compiler Error Message: CS0103: The name 'imageClass' does not exist in the current context
Line 46: <img src="@Umbraco.Media(item.relatedImage).umbracoFile" class="img-responsive @imageClass" alt="Responsive Image">
This is a rather stupid message to receive as I have clearly defined this variable already so it should be available for use. Can anyone point me in the right direction so that I can actually use my variable?
I think that it's perhaps because of the context that you have declared the variable in. Sometimes you need to declare the variables inside the html element where you need to use it rather than outside. Otherwise it seems it's not part of the proper scope...I think.
So I would try defining the variable just before the tag instead - does that help?
Hi all,
Thanks for the suggestions. I ended up doing this in the end:
var imageClass = item.GetPropertyValue<bool>("relatedImageTransparent") == true
? "transparentImage"
: string.Empty;
It's very frustrating that it's not intelligent enough to allow you to declare a variable in one place and use it in another within the same view but I guess that's just a flaw with Razor in general.
Variable does not exist in the current context
Hi all,
I am trying to add a class to my images depending on whether a checkbox is checked in the back office although I'm having some issues using the variables that I have defined.
The imageClass variable in my code above should apply an additional class to the image if the checkbox is checked in the back office. Otherwise it will append an empty string. However, when compiling and running my code I receive the following error:
This is a rather stupid message to receive as I have clearly defined this variable already so it should be available for use. Can anyone point me in the right direction so that I can actually use my variable?
Hi Jason
I think that it's perhaps because of the context that you have declared the variable in. Sometimes you need to declare the variables inside the html element where you need to use it rather than outside. Otherwise it seems it's not part of the proper scope...I think.
So I would try defining the variable just before the tag instead - does that help?
/Jan
Hello Jason,
You should declare the variable 'imageClass' outside the if statement.
Hi all, Thanks for the suggestions. I ended up doing this in the end:
It's very frustrating that it's not intelligent enough to allow you to declare a variable in one place and use it in another within the same view but I guess that's just a flaw with Razor in general.
Thanks for the other suggestions though.
There seems to be two solutions: Using
@Html.Raw()
to write tags, or adding a code block@{
…}
around code inside elements.does not work.
The two variants
or
works.
I suppose that using a code block inside elements is a requirement that is only indirectly enforced by the compiler.
I think I prefer the code block solution, as
Html.Raw()
gives me more freedom to mess up the HTML I generate.The code block solution worked for me.
is working on a reply...