← All lessons

Level 1 / ASP.NET Core Razor / lesson 2 of 6

Values, types, and ToString()

Render numbers, booleans, and derived values, and learn how .NET formats them.

Razor calls `ToString()` on whatever you hand it. That is why a `bool` renders as `True`, not `true` - a detail that trips up almost everyone the first time they see it in page source.

Understand the idea

Different values, different output

Razor writes the text form of an expression into the response. An int such as 6 appears as 6; a bool appears as True or False. That text may not be the wording you want to show a visitor.

Member access

@Model.Name.Length walks from the page model to a string and then to its length. Each member must exist on the type and the path must use the correct name.

Formatting is a decision

A real page can format numbers and dates for its audience rather than relying on default conversion. Do not rely on default bool text for labels like 'Staffed' or 'Unstaffed'.

7 lines
6 lines

Preview - simulated. Not a .NET compiler.

Ada Helibase

Crew:

Staffed:

Step 1 of 3

In progress

Add a number and a flag

Add an int property named Crew starting at 6, and a bool named IsStaffed starting as true.

  • StationModel declares an int property named Crew - not done yet
  • Crew starts at 6 - not done yet
  • StationModel declares a bool property named IsStaffed - not done yet
  • IsStaffed starts as true - not done yet

Coach

The checklist above is what decides whether a step is done. The coach only explains; it never grades.

The preview is a simulated Razor renderer, not a compiler. It supports @model, @{ } literal assignments, @Expression, @foreach, @if/@else, @* comments *@, and plain markup with class, id, and title attributes. Anything else is reported as outside the lesson instead of being guessed at.

Where the simulator stops

The simulator only handles a small set of value types and member paths. It does not model culture-specific formatting, nullable values, or full C# expressions.

Try it in a real .NET project

In a Razor Pages app, put a crew count and staffed flag on a PageModel. Render the count and show 'Staffed' or 'Unstaffed' based on the flag; change both values and confirm the rendered HTML changes.

Check your understanding

Why might displaying @Model.IsStaffed directly be poor UI even though it renders?

Show the explanation

The default string form is True or False. A visitor may need a clearer label, such as 'Staffed' or 'Unstaffed', and numbers or dates may need explicit formatting too.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.