Show object properties

You can use dotnet watch and observe the result of code changes instantly on the browser!

Display properties of an object

It is not recommended to declare classes in view code, they need to be declared in their respective Models directory or in page specific PageModel

@page

@{
    var joe = new Person()
    {
        Name = "Sam",
        Age = 25
    };
}
   
<p>Name: @joe.Name</p>
<p>Age: @joe.Age</p>

@functions{
    public class Person
    {        
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Display properties of a collection of objects

@page

@{
    var people = new List<Person>(){
        new Person()
        {
            Name = "Sam",
            Age = 25
        },
        new Person()
        {
            Name = "Ted",
            Age = 30
        }
    };
}
   
@foreach (var item in people)
{
    <p>Name: @item.Name</p>
    <p>Age: @item.Age</p>
}

@functions{
    public class Person
    {        
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Last updated