So I have an ASP.NET Core 2.1 MVC webapplication with viewmodels and razorviews that use those viewmodels.
In one of my Razor views I need to assign styling to an element based on a rating. I use the following code
public class MovieViewModel
{
public Movie Movie { get; set; }
public string GetRatingStyle(double? rating)
{
if (!rating.HasValue)
return "";
switch (rating)
{
case var _ when rating < 2.5:
return "low";
case var _ when rating >= 2.5 && rating < 5:
return "medium";
case var _ when rating >= 5 && rating < 7.5:
return "medium-high";
case var _ when rating >= 7.5:
return "high";
default:
return "";
}
}
}
I try to apply it in my Razor view with the following code
@model MovieViewModel
...
<div class="rating @{Model.GetRatingStyle(Model.Movie.Rating);}">
I can't seem to get this to work so is this the way to go or am I doing something stupid?