按钮中的禁用选项未动态更改

Disabled option in button is not being changed dynamically

我正在尝试根据我正在设置 OnInitialized 的值在按钮中设置禁用 属性。但即使将 EnableRegistration 设置为 true OnInitialized,该按钮仍保持禁用状态。如何根据 EnableRegistration 切换禁用 属性?

@page "/course/register"

<h3 class="my-5">Course Registration</h3>

<div class="container">
    <div class="alert alert-info">
        <p class="fw-bold">People who register for full course will be given priority over individual module registrations. 
            After registration, look for an email from us to see if you got in and instructions to pay the course fee.
            Registrations will open at 6:00 PM on March 20th.
        </p>
    </div>
    <div class=row>
    <div class="col-6">
        <button onclick="Register" id="fullcourse" disabled="@(EnableRegistration == true ? "false": "true")" class="btn btn-primary">Register for Full Course</button>
        
    </div>
    <div class="col-6">
        <button onclick="Register" id="fullcourse" disabled="@(EnableRegistration == true ? "false": "true")" class="btn btn-secondary">Register for First Module</button>
    </div><br/>
        @if (!string.IsNullOrEmpty(Message))
        {
            <p>@Message</p>
        }
    </div>
</div>


@code {
    public bool EnableRegistration;

    public bool Registered = false;

    public string Message = "";

    protected override void OnInitialized()
    {
        var registrationDateTime = new DateTime(2022, 3, 15, 6, 33, 0);

        if(Registered == false && (DateTime.Compare(DateTime.Now, registrationDateTime) == 1 || DateTime.Compare(DateTime.Now, registrationDateTime) == 0))
        {
            EnableRegistration = true;
        }
    }

    public void Register()
    {
        Message = "Registered";
    }
}

谢谢。

替换

 disabled="@(EnableRegistration == true ? "false": "true")"

 disabled="@(!EnableRegistration)"

HTML 不支持 ="true" 但 Blazor 支持。在最终结果中,您只需要 disabled 或一个空字符串。 razor 编译器会为您安排。

你需要

<button id="fullcourse" disabled="@(!EnableRegistration)"  class="btn btn-primary">Register for Full Course</button>