A rating is a radio button for each star in a fieldset. It posts with the form, and hovering fills the stars up to the pointer. For a score on a longer scale, use a Slider or a NumberField.
Saving a rating
A rating binds to int or int?, from 1 to Max (5 by default). The value posts as a number with the form.
Static
Preview
Code
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="rating-usage" OnValidSubmit="Save" MaxWidth="24.Rem" Grow>
<Rating @bind-Value="Model!.Stars" Hint="Click your rating again to clear it." />
<Row><Button Submit>Save</Button></Row>
if (_saved)
{
<Alert Status="Status.Success">Saved: (_stars is { } stars ? $"{stars} of 5 stars" : "no rating").</Alert>
}
</Form>
{
[SupplyParameterFromForm(FormName = "rating-usage")]
private ProductReview? Model { get; set; }
// What was saved, not the field: on an interactive page it follows each click.
private bool _saved;
private int? _stars;
protected override void OnInitialized() => Model ??= new() { Stars = 4 };
private void Save() => (_saved, _stars) = (true, Model!.Stars);
public sealed class ProductReview
{
[Display(Name = "Your rating")]
public int? Stars { get; set; }
}
}Static: Save posts the chosen star without a full reload.
Add this example to a page with the izzy CLI:
izzy add component Rating --at <file>:<line>A form post can send any number, so check the range on the server, with [Range(1, 5)]. Before trusting a posted value, see Values a form post can change.
Clearing a rating
Bound to int?, a rating can be empty. Clicking the chosen star clears it, and the arrow keys reach "No rating" before the first star, which screen readers announce. A required rating has no empty choice, as a checked radio would count as an answer.
Bound to int, a rating starts with no star chosen when the value is 0, and can't be cleared once one is.
Showing a rating
To show a rating someone can't change, such as an average, set ReadOnly. It takes any number, double too, and fills the stars to the exact fraction: 4.3 fills 86%.
Icon changes the star, Accent its color, and Max how many there are.
Static
Preview
Code
@* Read-only ratings show any value, filled to the exact fraction. *@
<Stack Gap="4">
<Rating Value="4.3" ReadOnly Label="Average of 128 reviews" />
<Rating Value="2.5" ReadOnly Label="Battery life" Size="ControlSize.Sm" />
<Rating Value="3" ReadOnly Label="Spice" Icon="Lucide.Flame" Accent="Accent.Danger" Max="4" />
</Stack>Screen readers hear one image, "4.3 out of 5 stars". In a Form, a read-only rating posts its value.
Sizes and disabled
To line a rating up with other fields, set Size. A Disabled rating can't be changed and doesn't post. In a read-only Form, a rating shows as a display.
Labels and localization
The names screen readers hear for the stars come from the app's text. To set them for the whole app, see Text and other languages.
| IzzyText | Sets | Default |
|---|---|---|
RatingOneStar | The first star's name | "1 star" |
RatingStars | The other stars' names; {0} is the number | "{0} stars" |
RatingNone | The empty choice's name | "No rating" |
RatingOutOf | A read-only rating's name; {0} is the value, {1} the number of stars | "{0} out of {1} stars" |
Mode support
| Feature | Static | Interactive |
|---|---|---|
| A radio for each star: arrow keys, posted with the form | Yes | Yes |
| Clicking the chosen star clears it | Yes | Yes |
| A read-only display filled to any value | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, clicking the chosen star doesn't clear it; the arrow keys reach "No rating".
Accessibility
- Screen readers hear a radio group named by its label, each star a radio named "1 star", "2 stars" and so on (
IzzyText.RatingOneStar,RatingStars); Tab reaches the group and the arrow keys move the choice. - The focus ring is drawn around the focused star, or around them all on "No rating" (
IzzyText.RatingNone). - A read-only rating is an image named "4.3 out of 5 stars" (
IzzyText.RatingOutOf). - After a post that fails with an error here, the chosen star (or the first) takes focus, as in
RadioGroup.
Parameters
| Prop | Type | Default |
|---|---|---|
MaxThe number of stars. | int | 5 |
IconThe icon for each star. | IconData? | computed |
AccentThe filled stars' color. | Accent? | computed |
ReadOnlyShows the value as stars filled to the exact fraction (4.3 fills 86%), an image to screen readers, and posts it. | bool | false |
@bind-ValueThe value, bound with @bind-Value. | TValue | |
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed. | string? |
Plus 28 field props.
Full APISee also
- RadioGroup: lets the user pick one choice from a list of radio buttons.
- Slider: lets the user pick a number by dragging along a range.
- SegmentedControl: lets the user pick one of a few choices from joined buttons.