Getting Started with the Rating
This tutorial explains how to set up the Telerik UI for ASP.NET MVC Rating and goes through the steps in the configuration of the component.
You will declare the Rating component. Then, you will configure the range of the Rating. Next, you'll set a default value and configure a label by using Kendo Templates. Finally, you will learn to handle the JavaScript events of the Rating and how to access client-side reference of the component.
After completing this guide, you will achieve the following results:
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET MVC HtmlHelpers:
@using Kendo.Mvc.UI
Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.
2. Initialize the Rating
Use the Rating HtmlHelper to configure the component.
- Use the
Name()
configuration method to assign a name to the instance of the helper—this is mandatory as its value is used for theid
and thename
attributes of the Rating element. - Configure the range of the Rating with the
Min
andMax
properties, and set a defaultValue
. - Use a Kendo Template to customize the
Label
of the component.
@using Kendo.Mvc.UI
@(Html.Kendo().Rating()
.Name("ratingLabelTemplate")
.Min(1)
.Max(6)
.Value(3)
.Label(l => l.Template("<span>#=value# out of #=maxValue#</span>"))
)
3. Handle the Rating Events
The Rating exposes a Change
event that you can handle and assign specific functions to the component. In this tutorial, you will use the Change
event to log a message in the browser's console when value of the Rating changes.
@using Kendo.Mvc.UI
@(Html.Kendo().Rating()
.Name("ratingLabelTemplate")
.Min(1)
.Max(6)
.Value(3)
.Label(l => l.Template("<span>#=value# out of #=maxValue#</span>"))
.Events(e=>e.Change("onChange"))
)
</div>
function onChange(e){
console.log(e.newValue)
}
(Optional) Reference Existing Rating Instances
Referencing existing component instances allows you to build on top of their configuration. To reference an existing Rating instance, use the jQuery.data()
method. Once a reference is established, use the Rating client-side API to control its behavior.
-
Use the
id
attribute of the component instance to establish a reference.<script> var ratingReference = $("#ratingExample").data("kendoRating"); // ratingReference is a reference to the existing instance of the helper. </script>
-
Use the Rating client-side API to control the behavior of the widget. In this example, you will see how to adjust the value of Rating programmatically.
<script> var rating = $("#trip").data("kendoRating"); rating.value(5); </script>