gpt4 book ai didi

c# - 无法在 Razor 页面中从 'method group' 转换为 'bool'

转载 作者:行者123 更新时间:2023-12-04 15:38:43 25 4
gpt4 key购买 nike

在尝试在 VS2019 for Mac 中构建 Blazor 博客引擎的公共(public)存储库时,我遇到以下编译错误,不知道为什么以及如何解决它:

Error CS1503: Argument 3: cannot convert from 'method group' to 'bool' (CS1503)



在以下 Razor 页面中:
@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>

最佳答案

<button>使@onclick方法没有 @ :

<button class="btn btn-primary" @onclick="Login">Login</button>

关于c# - 无法在 Razor 页面中从 'method group' 转换为 'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com