Skip to main content
Home  › ... Razor

Razor Basics Tutorials

Tutorial HomeRazor Basics

Basic set variable and show

var Firstname = "Terry" and show with @firstName

Just a simple variable - create and show.

⬇️ Result | Source ➡️

Name: Terry
@inherits Custom.Hybrid.RazorTyped
@{
  var firstName = "Terry";
}

Name: @firstName

⬇️ Result | Source ➡️

Age: 27
Birthday: 9/9/1999 12:00:00 AM
Birthday nicer using ToString("d"): 9/9/1999
Birthday as ISO ToString("yyyy-MM-dd"): 1999-09-09
@inherits Custom.Hybrid.RazorTyped

@{
  var age = 27;
  var birthday = new DateTime(1999, 09, 09);
}
Age: @age <br>
Birthday: @birthday <br>
Birthday nicer using ToString("d"): @birthday.ToString("d") <br>
Birthday as ISO ToString("yyyy-MM-dd"): @birthday.ToString("yyyy-MM-dd")

⬇️ Result | Source ➡️

Dog Count: 3
Dog[0]: hound
Dog First (with LINQ): hound
Dog Last (with LINQ): doggy
  1. Dog 0: hound
  2. Dog 1: dackel
  3. Dog 2: doggy
@inherits Custom.Hybrid.RazorTyped

@{
  var dogs = new string[] { "hound", "dackel", "doggy" };
}
<div>
  Dog Count: @dogs.Length <br>
  Dog[0]: @dogs[0] <br>
  Dog First (with LINQ): @dogs.First() <br>
  Dog Last (with LINQ): @dogs.Last() <br>
</div>

<ol>
  @for (int i = 0; i < dogs.Length; i++) {
    <li>Dog @i: @dogs[i]</li>
  }
</ol>