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
    0

    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";
          }else{
            var imageClass = string.Empty;
          }
    
          <div class="@containerClass">
            <section>
              <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>
                <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?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 22, 2014 @ 15:44
    Jan Skovgaard
    0

    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

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

    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
    100

    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
    1

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

      <p>
         String s = "s";
         @s
      </p>
    

    does not work.

    The two variants

    @Html.Raw("<p>")
        String s = "s";
        @s
    @Html.Raw("</p>")
    

    or

    <p>
       @{
          String s = "s";
          @s
       }
    </p>
    

    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.

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

    The code block solution worked for me.

Please Sign in or register to post replies

Write your reply to:

Draft