I'm trying to load an image within a webgrid within some if/else logic. Essentially if the API returns true show one image if false show the other image however the code I'm using right now -
grid.Column("PowerState", format: (vm) =>
if (vm.PowerState == "Stopped") //Choose font colour depending on the status of the VM
return new HtmlString("<img src ="~/Content/offstate.png">");
return new HtmlString("<img src =~/Content/onstate.png>");
return new HtmlString("<img src = " + Url.Content("~/Content/offstate.png") + ">");
You always need relative path to that image irrespective of current page. In such cases we need @Url.Content(...).
Look at why use @Url.Content topic and instead of providing path. Get path from @Url.Content and join to you html.