admin管理员组文章数量:1415476
I'm building a Blazor web application and having trouble binding form input fields to a model. When the user submits the form, the TrainingInfo.Course property is always null.
Problem:
When I enter text into the "Course Name" field and click the Submit button, the SubmitData() function is called, but TrainingInfo.Course is always null. I've double-checked the @bind-Value binding and TrainingInfo initialization but can’t figure out why the value isn't being updated.
Here’s the relevant code:
TrainingForm.razor
....
<EditForm Model="TrainingInfo" method="post" OnValidSubmit="SubmitData" FormName="trainingForm">
...
<div class="form-floating mb-3">
<InputText @bind-Value="TrainingInfo.Course" id="TrainingInfo.Course" class="form-control"
autocomplete="CourseName" aria-required="true" placeholder="CourseName" />
<label for="TrainingInfo.Course" class="form-label">Course Name</label>
<ValidationMessage For="() => TrainingInfo.Course" class="text-danger" />
</div>
...
<button type="submit" class="w-100 btn btn-lg btn-primary">Submit</button>
</EditForm>
...
@code {
[Parameter]
public TrainingInformationModel TrainingInfo { get; set; } = new();
[Parameter]
public EventCallback<bool> OnSaveSuccess { get; set; }
public async Task SubmitData()
{
Logger.LogInformation("CourseName: " + TrainingInfo.Course);
// This is where the issue is; TrainingInfo.Course is always null
}
}
Parent Component (TrainingInformationPage.razor)
@page "/TrainingPage"
...
<TrainingForm TrainingInfo="@TrainingInfo" OnSaveSuccess="@HandleSaveSuccess" />
...
@code {
private TrainingInformationModel TrainingInfo = new();
private void HandleSaveSuccess(bool success)
{
if (success)
{
Logger.LogInformation("Training information saved successfully.");
}
else
{
Logger.LogError("Training information failed to save.");
}
}
}
本文标签: aspnetBlazor InputText Field Bound to Model Always Null on SubmitStack Overflow
版权声明:本文标题:asp.net - Blazor InputText Field Bound to Model Always Null on Submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745173594a2646121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论