I've got the following snippet of Razor code, that exists in probably 15 different pages, that I'd like to reuse, if possible:

    <div class="col-xs-12">
        @if (@Model.Rating == 0)
        {
            <img src="/Images/Rating/NoRating.jpg" alt="" width="125">
        }
        else if (@Model.Rating == 1)
        {
            <img src="/Images/Rating/One.jpg" alt="" width="125">
        }
        else if (@Model.Rating == 2)
        {
            <img src="/Images/Rating/Two.jpg" alt="" width="125">
        }
        else if (@Model.Rating == 3)
        {
            <img src="/Images/Rating/Three.jpg" alt="" width="125">
        }
        else if (@Model.Rating == 4)
        {
            <img src="/Images/Rating/Four.jpg" alt="" width="125">
        }
        else if (@Model.Rating == 5)
        {
            <img src="/Images/Rating/Five.jpg" alt="" width="125">
        }
    </div>

What I would love to be able to do is to call a method and have the method return this code where I have it in my Razor .cshtml file. The method would also have to accept a parameter. In this case, the parameter would be a rating value of between 0 and 5. I would then replace all occurrences of @Model.Rating with the parameter value. Is it possible to do this? I'd rather not have to resort to a partial view if possible.

Related posts

Recent Viewed