【问题标题】:cannot convert from 'method group' to 'bool' in a Razor Page无法在 Razor 页面中从“方法组”转换为“布尔”
【发布时间】:2020-03-11 11:46:23
【问题描述】:

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

错误 CS1503:参数 3:无法从“方法组”转换为 '布尔'(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>

【问题讨论】:

标签: c# .net asp.net-core razor blazor


【解决方案1】:

更改&lt;button&gt;,使@onclick 方法没有@

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

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2013-10-02
    相关资源
    最近更新 更多