简体   繁体   English

Blazor 绑定输入值不适用于按钮单击

[英]Blazor binding input value not working on button click

I am using Blazor server side to do subscribe to a newsletter.我正在使用 Blazor 服务器端订阅时事通讯。 When I press the subscribe button, I see that the system has not bound the EmailAddress to the user entered email address.当我按下订阅按钮时,我看到系统没有将 EmailAddress 绑定到用户输入的电子邮件地址。 What am I missing?我错过了什么?

@inherits InstaTranscribeServerSide.Layouts.SubscribeToNewsletterComponent

<div class="container">
      <form class="mt-4">            
            <input type="email" name="email" placeholder="Enter your email address" text="@EmailAddress">
              <button type="button" @onclick="SubscribeToNewsletterClick">
              </button>
      </form>
</div>

public class SubscribeToNewsletterComponent : InstaTranscribeComponentBase
  {
    [Inject] protected ILogger<SubscribeToNewsletter> logger { get; set; }
    protected string EmailAddress { get; set; } = string.Empty;
    protected async Task SubscribeToNewsletterClick()
    {
      try
      {
        await this.EmailSender.AddUpdateContactAsync(EmailAddress);
        await JSRuntime.NotifySuccess($"You have been subscribed to our newsletter.");
      }
      catch (Exception exception)
      {
        await JSRuntime.NotifyError($"There was an error subscribing you to our newsletter.");
      }
    }
    protected override async Task OnInitializedAsync()
    {      
    }
}

screenshot showing emailaddress is blank:显示电子邮件地址为空的屏幕截图:

在此处输入图片说明

screenshot showing emailaddress entered by user:显示用户输入的电子邮件地址的屏幕截图:

在此处输入图片说明

Your page looks a little "un-Blazor" like.您的页面看起来有点像“非 Blazor”。 FYI here's a fairly classic Blazor version of your page.仅供参考,这是您页面的一个相当经典的 Blazor 版本。

It uses the Blazor EditForm components and an in-page status display.它使用 Blazor EditForm 组件和页内状态显示。

@page "/"
<h3>Subscribe To My Site</h3>
<EditForm EditContext=this.editContext OnSubmit=this.SubmitForm>
    <div class="col">
        <InputText class="form-control" typeof="email" @bind-Value=this.model.Email placeholder="Enter your email address" />
    </div>
    <div class="col m-2 p-2 text-end">
    <button class="btn btn-primary">Subscribe</button>
    </div>
</EditForm>
@if (this.isMessage)
{
    <div class="m-2">
        <div class="alert @this.alertCss">@this.message</div>
    </div>
}

@code {
    private NewsLetterCredentials model = new NewsLetterCredentials();

    private EditContext? editContext;

    private string message = string.Empty;
    private bool isMessage => !string.IsNullOrWhiteSpace(message);
    private string alertCss => this.success ? "alert-success" : "alert-danger";
    private bool success = true;

    protected override void OnInitialized()
        => this.editContext = new EditContext(model);

    private async Task SubmitForm()
    {
        await Task.Delay(200);
        // emulate save to database
        this.success = !this.success;
        this.message = this.success
            ? "Subscribed"
            : "Error Subscribing";

    }

    public class NewsLetterCredentials
    {
        public string Email { get; set; } = string.Empty;
    }
}

尝试使用@bind-value="EmailAddress"这是 blazor 中双向数据绑定的工作方式,您也可以将value="@EmailAddress"仅用于一种方式,您始终可以将输入作为参考并访问它的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM