Here’s the view:
@if (stream.StreamSourceId == 1)
{
<img class="source" src="@Url.Content("~/Public/assets/images/own3dlogo.png")" alt="" />
}
else if (stream.StreamSourceId == 2)
{
<img class="source" src="@Url.Content("~/Public/assets/images/twitchlogo.png")" alt="" />
}
Basically, I use a Model property to determine what image to render.
I know that the correct solution would be to create a property on the model called SourceImageUrl (string) and use that property as the source url for the image.
I would then transfer this conditional operation to the model.
My question is, how can I do this if I’m using DataAnnotations for validation? Any suggestions?
public class StreamModel
{
// This is the ID that has the value of either 1 or 2.
public int StreamSourceId { get; set; }
// How can I move the logic from the view, to here, and set the value accordingly?
public string SourceImageUrl { get; set; }
}
Can you not do something like this?