Implementing Star Rating in MVC Using AngularUI

In my previous post, I explained about:

In this post, I am going to explain about implementing Star Rating in MVC 5 Web Applications, using AngularUI, as a part of the AngularUI series.You may have seen in many websites, that they ask for feedback in the form of rating stars. No problem --  it very easy to implement. Just follow the below steps to create a StarRating system in your Web Application.

Implementing StarRating in MVC using AngularUI

  1. Create a Web Application using the MVC template (Here, I am using Visual studio 2015).
  2. It is better (Recommended) to implement an Application in Visual studio 2015 because VS2015 shows intelligence for Angular JS. This feature is not present in the previous versions.

The final output is like the screenshot shown below: output

Step 1: Add Controller and View

  1. Add a controller and name it (Here, I named it HomeController).
  2. Create an Index Action method and add view to it.
  3. Now, add the script ,given below, and reference file to an Index page.
    1. <script src="~/Scripts/angular.js"></script>  
    2.     <script src="~/Scripts/angular-animate.js"></script>  
    3.     <script src="~/Scripts/angular-ui/ui-bootstrap-tpls.js"></script>  
    4.     <script src="~/Scripts/app.js"></script><!--This is application script we wrote-->  
    5.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  

Step 2: Add Angular Script file

  1. Now, create another script file for Angular code to implement StarRating in the Application.
  2. Replace the Java Script file, with the code, given below:
    1. angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);  
    2. angular.module('ui.bootstrap.demo').controller('RatingDemoCtrl', function ($scope) {  
    3.     //These are properties of the Rating object  
    4.     $scope.rate = 7;    //gets or sets the rating value  
    5.     $scope.max = 10;    //displays number of icons(stars) to show in UI  
    6.     $scope.isReadonly = false;  //prevents the user interaction if set to true.  
    7.     $scope.hoveringOver = function (value) {  
    8.         $scope.overStar = value;  
    9.         $scope.percent = 100 * (value / $scope.max);  
    10.     };  
    11.     //Below are the rating states  
    12.     //These are array of objects defining properties for all icons.   
    13.     //In default template below 'stateOn&stateOff' properties are used to specify icon's class.  
    14.     $scope.ratingStates = [  
    15.       { stateOn: 'glyphicon-ok-sign', stateOff: 'glyphicon-ok-circle' },  
    16.       { stateOn: 'glyphicon-star', stateOff: 'glyphicon-star-empty' },  
    17.       { stateOn: 'glyphicon-heart', stateOff: 'glyphicon-ban-circle' },  
    18.       { stateOn: 'glyphicon-heart' },  
    19.       { stateOff: 'glyphicon-off' }  
    20.     ];  
    21. });  

Step 3: Create UI

  1. Replace and add the code, given below, in the Index.cshtml page.
    1. @{  
    2.     Layout = null;  
    3. }  
    4. <h2>Star Rating in Angualr UI</h2>  
    5. <!doctype html>  
    6. <html ng-app="ui.bootstrap.demo">  
    7. <head>     
    8.     <title>www.mitechdev.com</title>  
    9.     <script src="~/Scripts/angular.js"></script>  
    10.     <script src="~/Scripts/angular-animate.js"></script>  
    11.     <script src="~/Scripts/angular-ui/ui-bootstrap-tpls.js"></script>  
    12.     <script src="~/Scripts/app.js"></script><!--This is application script we wrote-->  
    13.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  
    14. </head>  
    15. <body style="margin-left:30px;">  
    16.     <div ng-controller="RatingDemoCtrl">  
    17.         <!--Angular element that shows rating images-->  
    18.         <uib-rating ng-model="rate" max="max"  
    19.                     read-only="isReadonly"   
    20.                     on-hover="hoveringOver(value)"  
    21.                     on-leave="overStar = null"  
    22.                     titles="['one','two','three']"   
    23.                     aria-labelledby="default-rating">  
    24.         </uib-rating>  
    25.         <!--span element shows the percentage of select-->  
    26.         <span class="label" ng-class="{'label-warning': percent<30, 'label-info': percent>=30 && percent<70, 'label-success': percent>=70}"  
    27.               ng-show="overStar && !isReadonly">{{percent}}%</span>  
    28.         <!--This element shows rating selected,Hovering and IS hovering or not(true/false)-->  
    29.         <pre style="margin:15px 0;width:400px;">Rate: <b>{{rate}}</b> - Readonly is: <i>{{isReadonly}}</i> - Hovering over: <b>{{overStar || "none"}}</b></pre>  
    30.         <!--button clears all the values in above <pre> tag-->  
    31.         <button type="button" class="btn btn-sm btn-danger" ng-click="rate = 0" ng-disabled="isReadonly">Clear</button>  
    32.         <!--this button toggles the selection of star rating-->  
    33.         <button type="button" class="btn btn-sm btn-default" ng-click="isReadonly = ! isReadonly">Toggle Readonly</button>  
    34.         <hr />  
    35.         <div>Mitechdev.com Application-2016</div>  
    36.     </div>  
    37. </body>  
    38. </html>  
  2. Here, we need to talk about some expressions used in <uib-rating> tag.

    • on-hover: This expression is called, when the user places the mouse at the particular icon. In the above code hoveringOver() is called.
    • on-leave: This expression is called when the user leaves the mouse at the particular icon.
    • titles: Using this expression, we can assign an array of the titles to each icon.

Download the source code here.

Conclusion

I hope this tutorial is helpful for many readers and developers.

Up Next
    Ebook Download
    View all
    Learn
    View all