简体   繁体   中英

c# razor if statement not working

I am being asked do make some changes to a webpage buid with Umbraco of which I have almost no knowledge and a very basic understanding of C#.

This is the offending bit of the code:

@foreach (DynamicNode child in root.Children)
        {
            string image = @child.GetProperty("image").Value;
            var imgURL = Model.MediaById(image).url;
            var extURL = @child.GetProperty("externalURL").Value;
            var fullURL = "http://" + extURL;

            string externalLinks = child.GetProperty("externalDocuments").Value;
            string downloadsFolderId = @child.GetProperty("downloadsFolder").Value;
            string brandName = child.GetProperty("brandName").Value;
            bool brandCheck = brandName.Contains("Generic");

            if (brandCheck == true)
            {
              string brandHeader = "<h2 class='blue' title='"+@child.GetProperty("brandName").Value+"'>"+@child.GetProperty("brandName").Value+" <span></span></h2>";
            }
            else
            {
              string brandHeader = "<h2 class='red' title='"+@child.GetProperty("brandName").Value+"'>"+@child.GetProperty("brandName").Value+" <span></span></h2>";
            }

            //Links
            <span style="display:none" id="tester" >@brandCheck</span>
            <div class="brand-container">
                @Html.Raw(brandHeader)

So essentially I need to check if the returned name of product contains string "Generic". If it does, apply class "blue", if not apply class "red" - sound easy enough.

The string search works ok because the hidden @brandCheck return true or false values accordingly. Also if I remove the if statement and define the string brandHeader, it will be correctly generated on the (in the above code) last line (@html.Raw(brandHeader).

Clearly the issue is the if statement. I tried:

  • if(brandCheck == true)
  • if(brandCheck === true)
  • if(brandCheck == 'true')
  • if(brandCheck === 'true')
  • if(brandCheck)

but nothing seems to work, the if statement will crash the script. What am I missing or doing wrong?

Thanks for any help

Try and replace

bool brandCheck = brandName.Contains("Generic");

if (brandCheck == true)
{
     string brandHeader = "<h2 class='blue' title='"+@child.GetProperty("brandName").Value+"'>"+@child.GetProperty("brandName").Value+" <span></span></h2>";
}
else
{
     string brandHeader = "<h2 class='red' title='"+@child.GetProperty("brandName").Value+"'>"+@child.GetProperty("brandName").Value+" <span></span></h2>";
}

with

string brandHeader = string.Format("<h2 class='{0}' title='"+@child.GetProperty("brandName").Value+"'>"+@child.GetProperty("brandName").Value+" <span></span></h2>", brandName.Contains("Generic") ? "blue" : "red");

It's not the if-clause crashing your program, but rather the fact that your

string brandHeader 

is only defined inside the if clause.

If you access it later on, it is not defined anymore, causing your script to crash (In other words: As soon as you leave your if-clause, your string brandHeader is out of scope).

As a workaround, put the

string brandHeader;

outside your if-clause, and in the if-clause only assign the value by

brandheader = < value >;

without the string at the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM