c# - EditForm 需要模型参数或 EditContext 参数

标签 c# blazor-server-side

如何解决此 Blazor 服务器错误?

EditForm requires either a Model parameter, or an EditContext parameter

我在下面创建了一个可重现性最低的示例。问题似乎是因为模型未实例化...为什么以下简单页面会抛出此错误?

@page "/"
<EditForm @Model="@person">
    <input @bind="@person.FirstName" />
    <input @bind="@person.LastName" />
</EditForm>
@code 
{
    public Person person = new Person();
    protected override Task OnInitializedAsync()
    {
        person = new Person
        {
            FirstName = "Fred",
            LastName = "Flintstone"
        };
        return base.OnInitializedAsync();
    }
}

最佳答案

我不仅可以告诉你错误,还可以告诉你如何在 VS 中输入时检查它。

更改为:

<EditForm Model="@person">

(即 Model 上没有“@”符号)

当您输入 <Editform> 时行,当您按空格键时,您会看到预期变量列表,其中有几个以 @ 开头, 比如 @ref ,还有一些没有,比如 Model .

关于c# - EditForm 需要模型参数或 EditContext 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67641707/

相关文章:

c# - 关于 ComboBoxes、ValueMember 和 DisplayMember 的一些混淆

c# - 在 C# Ilist 中排队

c# - Blazor 服务器方法调用和执行详细信息

blazor - OnInitializedAsync 方法在更改查询字符串值时没有被调用

asp.net - 获取 Blazor Server 中的当前 Windows 用户

c# - 从代码 (C#) 使用 FontIcon Glyph

c# - 如何修复 "Element is Currently not Visible"Selenium <a> 元素

c# - FullCalendar 事件添加和更新示例

c# - 在 ASP.NET Core Web 应用程序中使用 Identity 登录期间向用户添加声明

blazor - 如何从子组件获取对 Blazor MainLayout 的引用