Home > Net >  How do I change <img src=""> value conditionally? | Razor CSHTML
How do I change <img src=""> value conditionally? | Razor CSHTML

Time:04-04

I'd like to set the src value to ImgLink if the number of elements in my iList is >= 7 and set it to an empty string if not.

I tried this

<div><img [email protected] >= 7 ? "@UrlResolver.Current.GetUrl(Model.SlideshowItems[6].ImgLink)" : ""></div>

But this error shows up:

CodePudding user response:

You can do something like:

@{
    string src = "img/exampleDefault.png";
    if (//logic)
    {
        src = "img/example2.png";
    }
}

<div><img src=@src></div>

you can reduce the logic to one line:

@{
    string src = //logic ? "img/example1.png" : "img/example2.png";
}
  • Related