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

Viewed 146

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 src=@Model.SlideshowItems.Count >= 7 ? "@UrlResolver.Current.GetUrl(Model.SlideshowItems[6].ImgLink)" : ""></div>

But this error shows up:

1 Answers

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