我正在使用 Blazor 和 MudBlazor 创建一个简单的注册页面,我最终可以将用户信息作为 JSON 对象传递给我的服务器项目。作为 Blazor 的新手,我选择使用 EditForm 方法,因为它是 Blazor 组件而不是 MudBlazor 特有的。
我在这里遵循了文档:https ://mudblazor.com/components/form#editform-support
当我导航到我的登录页面时,我收到一个错误。EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.
我所做的广泛研究坚持认为对象没有被正确实例化,但我几乎肯定我已经正确地做到了这一点。我将我的文件分成一个 ModelView 和一个 CodeBehind 页面,但即使我在单个代码块中创建了所有内容,我也得到了相同的结果。这是代码
注册剃须刀
<EditForm SignUpVM="@MySignUpVM" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<MudGrid>
<MudItem xs="12" sm="7">
<MudCard>
<MudCardContent>
<MudTextField Label="First name" HelperText="Max. 8 characters"
@bind-Value="MySignUpVM.Username" For="@(() => MySignUpVM.Username)"/>
<MudTextField Label="Email" Class="mt-3"
@bind-Value="MySignUpVM.EmailAddress" For="@(() => MySignUpVM.EmailAddress)"/>
<MudTextField Label="Password" HelperText="Choose a strong password" Class="mt-3"
@bind-Value="MySignUpVM.Password" For="@(() => MySignUpVM.Password)" InputType="InputType.Password"/>
<MudTextField Label="Password" HelperText="Repeat the password" Class="mt-3"
@bind-Value="MySignUpVM.ConfPassword" For="@(() => MySignUpVM.ConfPassword)" InputType="InputType.Password"/>
</MudCardContent>
<MudCardActions>
<MudButton Variant="Variant.Filled" Color="Color.Primary" DisableElevation="true" Class="mx-2" Link="/signin">Back</MudButton>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Register</MudButton>
</MudCardActions>
</MudCard>
</MudItem>
</MudGrid>
SignUp.razor.cs
using Client.ViewModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
namespace Client.Pages
{
public partial class SignUp : ComponentBase
{
bool success;
public SignUpVM MySignUpVM { get; set; } = new SignUpVM();
public void OnValidSubmit(EditContext context)
{
success = true;
StateHasChanged();
}
}
}
注册虚拟机.cs
using Client.ViewModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
namespace Client.Pages
{
public partial class SignUp : ComponentBase
{
bool success;
public SignUpVM MySignUpVM { get; set; } = new SignUpVM();
public void OnValidSubmit(EditContext context)
{
success = true;
StateHasChanged();
}
}
}
我在这个项目中使用 .NET6。