我正在使用Blazor和MudBrazor创建一个简单注册页面,最终可以将用户信息作为JSON对象传递给我的服务器项目。作为Blazor的新手,我选择使用EditForm方法,因为它是Blazor组件,而不是MudBrazor特有的。
我遵循了此处的文档:https://mudblazor.com/components/form#editform-支持
当我导航到登录页面时,会出现一个错误。EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.
我所做的大量研究坚持认为对象没有被正确实例化,但我几乎肯定我做得正确。我将文件分为ModelView和CodeBehind页面,但即使在一个代码块中创建了所有内容,我也会得到相同的结果。这是代码
注册。Razor
<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();
}
}
}
SignUpVM.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。
EditForm需要一个Model参数,这一错误清楚地表明了这一点。只需提供一个模型实例
<EditForm Model="ClassInstance" SignUpVM="@MySignUpVM" OnValidSubmit="OnValidSubmit">