Koosh
Koosh

Reputation: 896

how to reference a property from a class in Blazor

I have a simple Blazor application.

I have a label in my HTML portion of the .Razor page. In the @code section, I have a public class with a few properties, and I'm trying to simply display the values from my class in the labels.

Code below:

<label>@How To Display EmailAddressHere</label>

<label>@How To Display Body</label>

@code {

    public class EmailItems
    {
    public string EmailAddress { get; set; }
    public string Body { get; set; }
    }

    protected override void OnInitialized()
    {

        var emailItems = new EmailItems()
            {
                Body = "testBody",
                EmailAddress= "jdoe@gmail.com",
            };
    }
}

Upvotes: 2

Views: 1451

Answers (1)

Brian Parker
Brian Parker

Reputation: 14623

You need to make a field or a property of type EmailItems and assign that instead of a var.

<label>@emailItem.EmailAddress </label>

<label>@emailItem.Body </label>

@code {

    EmailItems emailItem;

    public class EmailItems
    {
        public string EmailAddress { get; set; }
        public string Body { get; set; }
    }

    protected override void OnInitialized()
    {

            this.emailItem = new EmailItems()
            {
                Body = "testBody",
                EmailAddress= "jdoe@gmail.com",
            };
    }
}

Upvotes: 2

Related Questions