Spider Man
Spider Man

Reputation: 425

How to display the stars according to the ratings in html view in ASP.NET MVC?

I am building an asp.net mvc application. I display the ratings of a resource on a page using stars. Depending on the average rating the equivalent number of stars should be displayed.

I have written out the following code:

<span class="glyphicon glyphicon-star"></span> // displays 1 star
<span class="glyphicon glyphicon-star-empty"></span> // display 1 empty star

// Displays the average ratings float value
@foreach(var resource in ViewBag.Resources)
{
  <p>@resource.Ratings</p>
}

I want something like this to be displayed

@foreach(var resource in ViewBag.Resources)
{
  @resource.Ratings * <span class="glyphicon glyphicon-star" /> ([email protected]) * <span class="glyphicon glyphicon-star-empty" />
}

How can this be done? Thanks

Upvotes: 0

Views: 7296

Answers (2)

Spider Man
Spider Man

Reputation: 425

@for (int i = 0; i < Int32.Parse(@resource.Ratings); i++)
{ 
     <span class="glyphicon glyphicon-star"></span>
}
@for (int i = Int32.Parse(@resource.Ratings); i < 5; i++ )
{ 
     <span class="glyphicon glyphicon-star-empty"></span>
}
@resource.Ratings stars

Upvotes: 6

Ahmed
Ahmed

Reputation: 1590

Use JavaScript Library for displaying stars in efficient way and good UI, you have many options Five stars rating bootstrap-star-rating

Upvotes: 1

Related Questions