admin管理员组

文章数量:1025259

Ok, so I am VERY NEW at Blazor (coming from old school Webforms), and have a simple Web App (InteractiveAuto) where I am testing simply binding values to a class. It's not working, when I tab out of the MudTextFields the corresponding MudText does not update. I know it is a very noob situation, but can anyone slap me a tell me why?

@page "/"

<PageTitle>Home</PageTitle>

<MudAlert Severity="Severity.Error">You are delinquent on your payments.</MudAlert>

<h1>Hello, world!</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<br />
<MudTextField T="string" @bind-Value="testClass.Name"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter"></MudTextField>

Welcome to your new app.

@code {
    MyData testClass = new MyData
    {
        Id = 1,
        Name = "Jimmy",
        Counter = 5
    };

    class MyData
    {
        public int Id { get; set; }
        public string Name { get; set; } = "";
        public int Counter { get; set; }
    }
}

Ok, so I am VERY NEW at Blazor (coming from old school Webforms), and have a simple Web App (InteractiveAuto) where I am testing simply binding values to a class. It's not working, when I tab out of the MudTextFields the corresponding MudText does not update. I know it is a very noob situation, but can anyone slap me a tell me why?

@page "/"

<PageTitle>Home</PageTitle>

<MudAlert Severity="Severity.Error">You are delinquent on your payments.</MudAlert>

<h1>Hello, world!</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<br />
<MudTextField T="string" @bind-Value="testClass.Name"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter"></MudTextField>

Welcome to your new app.

@code {
    MyData testClass = new MyData
    {
        Id = 1,
        Name = "Jimmy",
        Counter = 5
    };

    class MyData
    {
        public int Id { get; set; }
        public string Name { get; set; } = "";
        public int Counter { get; set; }
    }
}
Share Improve this question asked Dec 17, 2024 at 15:53 Robert BenedettoRobert Benedetto 1,7105 gold badges32 silver badges58 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Had to add a form and now it works.

<MudForm Model="@testClass"></MudForm>

I see you've answered yourself but you don't need to add a form to bind values to a class object.

In MudBlazor the input components by default only get their values bound when you press Enter or when it loses focus i.e. The input is not selected anymore.

If you add the Immediate="true" property to the component then it will bind immediately as you type. This works for any input component in MudBlazor.

<h1>Bind immediate</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<hr/>

<MudTextField T="string" @bind-Value="testClass.Name" Immediate="true"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter" Immediate="true"></MudTextField>

Demo Snippet

Ok, so I am VERY NEW at Blazor (coming from old school Webforms), and have a simple Web App (InteractiveAuto) where I am testing simply binding values to a class. It's not working, when I tab out of the MudTextFields the corresponding MudText does not update. I know it is a very noob situation, but can anyone slap me a tell me why?

@page "/"

<PageTitle>Home</PageTitle>

<MudAlert Severity="Severity.Error">You are delinquent on your payments.</MudAlert>

<h1>Hello, world!</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<br />
<MudTextField T="string" @bind-Value="testClass.Name"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter"></MudTextField>

Welcome to your new app.

@code {
    MyData testClass = new MyData
    {
        Id = 1,
        Name = "Jimmy",
        Counter = 5
    };

    class MyData
    {
        public int Id { get; set; }
        public string Name { get; set; } = "";
        public int Counter { get; set; }
    }
}

Ok, so I am VERY NEW at Blazor (coming from old school Webforms), and have a simple Web App (InteractiveAuto) where I am testing simply binding values to a class. It's not working, when I tab out of the MudTextFields the corresponding MudText does not update. I know it is a very noob situation, but can anyone slap me a tell me why?

@page "/"

<PageTitle>Home</PageTitle>

<MudAlert Severity="Severity.Error">You are delinquent on your payments.</MudAlert>

<h1>Hello, world!</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<br />
<MudTextField T="string" @bind-Value="testClass.Name"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter"></MudTextField>

Welcome to your new app.

@code {
    MyData testClass = new MyData
    {
        Id = 1,
        Name = "Jimmy",
        Counter = 5
    };

    class MyData
    {
        public int Id { get; set; }
        public string Name { get; set; } = "";
        public int Counter { get; set; }
    }
}
Share Improve this question asked Dec 17, 2024 at 15:53 Robert BenedettoRobert Benedetto 1,7105 gold badges32 silver badges58 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Had to add a form and now it works.

<MudForm Model="@testClass"></MudForm>

I see you've answered yourself but you don't need to add a form to bind values to a class object.

In MudBlazor the input components by default only get their values bound when you press Enter or when it loses focus i.e. The input is not selected anymore.

If you add the Immediate="true" property to the component then it will bind immediately as you type. This works for any input component in MudBlazor.

<h1>Bind immediate</h1>
<MudText>@testClass.Id</MudText>
<MudText>@testClass.Name</MudText>
<MudText>@testClass.Counter</MudText>
<hr/>

<MudTextField T="string" @bind-Value="testClass.Name" Immediate="true"></MudTextField>
<MudTextField T="int" @bind-Value="testClass.Counter" Immediate="true"></MudTextField>

Demo Snippet

本文标签: blazorTrying to bind value and displaynot workingStack Overflow