【问题标题】:Disable/Enable Button in Blazor - Depending on what is in the formBlazor 中的禁用/启用按钮 - 取决于表单中的内容
【发布时间】:2020-12-25 10:17:44
【问题描述】:

所以我在 ASP.NET Core 中创建了这个小项目,我有一个已经编写好的 WEB API,但我正在努力使用 Blazor 中的前端来使用该 API。 POST、GET HTTP 请求效果很好。我有一个剃须刀页面,我在其中放入了一些数据(姓名、家庭名称等),然后单击发送,数据被发布到 API。

当涉及到该表单时,有一些验证: 姓名 – 至少 5 个字符 FamilyName – 至少 5 个字符 地址 - 至少 10 个字符 EmailAdress – 必须是有效的电子邮件 年龄 - 必须在 20 到 60 岁之间

这一切都通过 DataAnnotations 完成:

using System.ComponentModel.DataAnnotations;

namespace Blazor.Data
{
    public class Applicant
    {
        public int Id { get; set; }

        [MinLength(5, ErrorMessage ="Name must contain atleast 5 characters.")]
        public string Name { get; set; }

        [MinLength(5, ErrorMessage ="Family Name must contain atleast 5 characters.")]
        public string FamilyName { get; set; }

        [MinLength(10,ErrorMessage ="Address must contain atleast 10 characters.")]
        public string Address { get; set; }

        public string CountryOfOrigin { get; set; }

        [EmailAddress(ErrorMessage ="E-Mail adress is not valid.")]
        public string EmailAddress { get; set; }

        [Range(20,60,ErrorMessage ="Age must be between 20 and 60.")]
        public int Age { get; set; }

        public bool Hired { get; set; }
    }
}

在 Razor 页面中,我有一个表格要填写,然后发送到 API,如下所示:

@page "/postapplicant"
@using Blazor.Data
@using System.Web
@inherits ApplicantCreateBase

<h1>Create an Applicant</h1>

<p>This component demonstrates posting a data to a Web API.</p>

<EditForm Model="@Applicant" OnValidSubmit="@SendValid">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <hr />
    <div class="form-group row">
        <label for="Name" class="col-sm-2 col-form-label">
            First Name
        </label>
        <div class="col-sm-10">
            <InputText id="Name" class="form-control" placeholder="First Name"
                       @bind-Value="Applicant.Name" />
            <ValidationMessage For="@(() =>Applicant.Name)" />
        </div>
    </div>
    <div class="form-group row">
        <label for="FamilyName" class="col-sm-2 col-form-label">
            Family Name
        </label>
        <div class="col-sm-10">
            <InputText id="FamilyName" class="form-control" placeholder="Family Name"
                       @bind-Value="Applicant.FamilyName" />
            <ValidationMessage For="@(() =>Applicant.FamilyName)" />
        </div>
    </div>
    <div class="form-group row">
        <label for="Address" class="col-sm-2 col-form-label">
            Address
        </label>
        <div class="col-sm-10">
            <InputText id="Address" class="form-control" placeholder="Address"
                       @bind-Value="Applicant.Address" />
            <ValidationMessage For="@(() =>Applicant.Address)" />
        </div>
    </div>
    <div class="form-group row">
        <label for="CountryOfOrigin" class="col-sm-2 col-form-label">
            Country
        </label>
        <div class="col-sm-10">
            <InputSelect id="CountryOfOrigin" class="form-group" placeholder="Country Of Origin"
                         @bind-Value="Applicant.CountryOfOrigin">
                @foreach (var item in Countries)
                {
                    <option>@item.Title</option>
                }
            </InputSelect>
        </div>
    </div>
    <div class="form-group row">
        <label for="EMailAddress" class="col-sm-2 col-form-label">
            E-Mail Address
        </label>
        <div class="col-sm-10">
            <InputText id="EMailAddress" class="form-control" placeholder="E-Mail Address"
                       @bind-Value="Applicant.EmailAddress" />
            <ValidationMessage For="@(() =>Applicant.EmailAddress)" />
        </div>
    </div>
    <div class="form-group row">
        <label for="Age" class="col-sm-2 col-form-label">
            Age
        </label>
        <div class="col-sm-10">
            <InputNumber id="Age" class="form-control" placeholder="Age"
                         @bind-Value="Applicant.Age" />
            <ValidationMessage For="@(() =>Applicant.Age)" />
        </div>
    </div>
    <div class="form-group row">
        <label for="Hired" class="col-sm-2 col-form-label">
            Hired
        </label>
        <div class="col-md-1">
            <InputCheckbox id="Hired" class="form-control" placeholder="Hired"
                           @bind-Value="Applicant.Hired" />
        </div>
    </div>
    <button class="btn btn-primary"  type="submit">Send</button>
    <button class="btn btn-secondary" type="button" @onclick="Reset_Click">Reset</button>

</EditForm>
<Confirm ConfirmationChanged="ConfirmReset_Click" @ref="ResetConfirmation"></Confirm>

一切正常且按预期工作,但我希望仅当整个表单根据我上面列出的规则有效时才启用发送按钮。我知道您可以在按钮中使用这个禁用的属性,但我不知道如何正确实现它。在 C# / .net 核心中,这似乎是一团糟。从头开始编写 web api 更容易。帮助将不胜感激,谢谢!

【问题讨论】:

    标签: c# button frontend blazor webapi


    【解决方案1】:

    您可以访问EditForm 的上下文:

      <button class="btn btn-primary" type="submit" disabled="@(!context.Validate())">Send</button>
    

    【讨论】:

    • 这比我想象的要简单。非常感谢!
    • 这不会在表单生效后立即启用按钮。为此,您必须在代码中跟踪表单有效性并将禁用属性绑定到布尔值,然后在表单变为有效时立即切换布尔值。然后做 StateHasChanged()。如果我错了,请纠正我。
    • 谢谢,很好用!我也包含IsModified,所以如果表单无效或尚未修改,则提交被禁用:disabled="@(!context.Validate() || !context.IsModified())"
    【解决方案2】:

    为了避免在表单尚未修改(即最初)时显示验证错误消息,您可以使用:

    Disabled="@(!context.IsModified() || !context.Validate())"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      相关资源
      最近更新 更多