试图在VS2019 for Mac中构建Blazor博客引擎的公共仓库时,遇到以下编译错误,并且不知道为什么以及如何解决它:
错误CS1503:参数3:无法从“方法组”转换为 '布尔'(CS1503)
在以下剃刀页面中:
@layout MainLayout
@inherits LoginModel
<WdHeader Heading="WordDaze" SubHeading="Please Enter Your Login Details"></WdHeader>
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4">
<div class="editor">
@if (ShowLoginFailed)
{
<div class="alert alert-danger">
Login attempt failed.
</div>
}
<input type="text" @bind=@LoginDetails.Username placeholder="Username" class="form-control" />
<input type="password" @bind=@LoginDetails.Password placeholder="Password" class="form-control" />
<button class="btn btn-primary" @onclick="@Login">Login</button>
</div>
</div>
</div>
CS文件如下:
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using WordDaze.Shared;
namespace WordDaze.Client.Features.Login
{
public class LoginModel : ComponentBase
{
[Inject] private AppState _appState { get; set; }
[Inject] private NavigationManager _navigationManager { get; set; }
protected LoginDetails LoginDetails { get; set; } = new LoginDetails();
protected bool ShowLoginFailed { get; set; }
protected async Task Login()
{
await _appState.Login(LoginDetails);
if (_appState.IsLoggedIn)
{
_navigationManager.NavigateTo("/");
}
else
{
ShowLoginFailed = true;
}
}
}
}
您能解释一下为什么会发生这种情况以及如何解决吗?
解决方案:
<button class="btn btn-primary" @onclick="@Login">Login</button>
缺少登录方法的括号:
<button class="btn btn-primary" @onclick="@Login()">Login</button>
答案 0 :(得分:0)
更改 <button>
以便 @onclick
方法没有 @
:
<button class="btn btn-primary" @onclick="Login">Login</button>