Blazor(服务器端)错误未捕获引用错误:未定义LoginContributor



遇到一个奇怪的问题。这是一个Blazor应用程序(服务器端(,使用.NET Core 3.1

我创建了一个新的Blazor应用程序来复制这个。我修改了反例,并将其修改为以下内容:

@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<EditForm Model="@loginData" OnValidSubmit="LoginContributor">
<div class="login-section">
<div class="row">
<div class="col">
<label for="UserNameEdit" class="col-form-label">Username:</label>
<InputText id="UserNameEdit" @bind-Value="loginData.Username"></InputText>
</div>
<div class="col">
<label for="PasswordEdit" class="col-form-label">Password:</label>
<InputText id="PasswordEdit" @bind-Value="loginData.Password" type="password"></InputText>
</div>
<div class="col">
<button id="LoginButton" class="btn btn-primary" onclick="LoginContributor">Log In</button>
</div>
</div>
<div class="row">
<div class="col">
<InputCheckbox id="SaveLoginEdit" @bind-Value="loginData.SaveLoginInformation"></InputCheckbox>
<label for="SaveLoginEdit" class="col-form-label">Save login information</label>
</div>
</div>
</div>
</EditForm>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
public class LoginObject
{
public string Username = "";
public string Password = "";
public bool SaveLoginInformation = false;
}
private LoginObject loginData = new LoginObject();
private int currentCount = 0;
private void LoginContributor()
{
IncrementCount();
}
private void IncrementCount()
{
currentCount++;
}
}

如果我点击";点击我";按钮,我在控制台上没有得到任何错误,但如果我点击";登录";按钮,我得到以下控制台错误:

counter:1 Uncaught ReferenceError: LoginContributor is not defined
at HTMLButtonElement.onclick (counter:1)

代码仍然执行良好,但我不喜欢控制台错误,我想知道它为什么会出现以及如何修复

谢谢!

这是因为您在按钮上使用onclick而不是@onclickonclick期望javascript代码,@onclick期望c#函数
右:
<button id="LoginButton" class="btn btn-primary" @onclick="LoginContributor">Log In</button>

最新更新