Copied to clipboard

Flag this post as spam?

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

  • Jason Espin 368 posts 1335 karma points
    Apr 22, 2014 @ 15:33
    Jason Espin

    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.

    @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";
            var imageClass = string.Empty;
          <div class="@containerClass">
              <h2><a href="@item.Url" class="blogLink">@title</a></h2>
              <span class="glyphicon glyphicon-time">&nbsp;</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 class="col-md-7 textContent">
                  @if (string.IsNullOrWhiteSpace(item.subheader) == false)
                  @Umbraco.Truncate(item.bodyText, 200)
                  <a href="@item.Url" class="btn btn-primary">Continue Reading</a>

    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?

  • Jan Skovgaard 11256 posts 23495 karma points MVP 7x admin c-trib
    Apr 22, 2014 @ 15:44
    Jan Skovgaard

    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?


  • Wesley Herpoelaert 52 posts 123 karma points
    Apr 22, 2014 @ 15:50
    Wesley Herpoelaert

    Hello Jason,

    You should declare the variable 'imageClass' outside the if statement.

    var imageClass = item.relatedImageTransparent ? "backgroundFraming" : string.Empty;
  • Jason Espin 368 posts 1335 karma points
    Apr 22, 2014 @ 15:54
    Jason Espin

    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.

    Thanks for the other suggestions though.

  • Jan Egil Kristiansen 37 posts 160 karma points
    Jun 27, 2017 @ 14:00
    Jan Egil Kristiansen

    There seems to be two solutions: Using @Html.Raw() to write tags, or adding a code block @{} around code inside elements.

         String s = "s";

    does not work.

    The two variants

        String s = "s";


          String s = "s";


    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.

  • Echo Train 16 posts 106 karma points
    Aug 02, 2018 @ 14:46
    Echo Train

    The code block solution worked for me.

Please Sign in or register to post replies

Write your reply to: