简体   繁体   English

C# Blazor 和数据绑定到日期时间?

[英]C# Blazor and data binding to a DateTime?

I have to add data validation for a Date and Time input fields for an existing Asp.net Core 3.1 Blazor project;我必须为现有的Asp.net Core 3.1 Blazor项目的日期和时间输入字段添加数据验证; this is my first time working with Blazor.这是我第一次与 Blazor 合作。

I have setup data binding on the date input field (which has a Date picker and data bound to a DateTime variable) with no problems.我在日期输入字段(它有一个日期选择器和绑定到 DateTime 变量的数据)上设置了数据绑定,没有问题。 However, the case is not the same for the time input (which has a Time picker and is bound to another DateTime variable.)但是,时间输入的情况不同(它有一个时间选择器并绑定到另一个 DateTime 变量。)

In short, when the user uses the time picker to set the time and closes it, the time in the input field goes back to the time that it was originally set to.简而言之,当用户使用时间选择器设置时间并关闭它时,输入字段中的时间将回到最初设置的时间。 For example, if the time input is currently set at 12:00 AM and then user selects a new time from the Time picker (lets say 2:00 PM), the user clicks ok and closes the Time picker, the time in the input goes back to 12:00 AM.例如,如果时间输入当前设置为上午 12:00,然后用户从时间选择器中选择一个新时间(假设是下午 2:00),则用户单击确定并关闭时间选择器,即输入中的时间回到 12:00 AM。 I have another time input on the same page but data bound to a string instead of DateTime, and if the time is set again in the Time picker, the time is preserved.我在同一页面上有另一个时间输入,但数据绑定到一个字符串而不是 DateTime,如果在时间选择器中再次设置时间,时间将被保留。

I wrote a very short program to demonstrate the problem.我写了一个很短的程序来演示这个问题。 My sample page has three input fields: Date, Time and another Time but its data bind to a string.我的示例页面有三个输入字段:日期、时间和另一个时间,但它的数据绑定到一个字符串。

在此处输入图片说明

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<div class="wrapper">
  <section class="createevent">
    <EditForm EditContext="@_editContext" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
      <DataAnnotationsValidator />
      <div class="form-group">
        <p>
          <label>
            Date:
          </label>
          <input id="txtDate" type="date" required @bind-value="_timeSample.date" />
          <ValidationMessage For="@(() => _timeSample.date)" />
        </p>
      </div>
      <div class="form-group">
        <p>
          <label>
            Time:
          </label>
          <input id="txtTime" type="time" required @bind="_timeSample.time" />
          <ValidationMessage For="@(() => _timeSample.time)" />
        </p>
      </div>
      <div class="form-group">
        <p>
          <label>
            Time2:
          </label>
          <InputText id="txtTime2" type="time" required @bind-Value="_timeSample.text" />
          <ValidationMessage For="@(() => _timeSample.text)" />
        </p>
      </div>
    </EditForm>
  </section>
</div>

@code
{
  public class TimeSample
  {
    public DateTime date { get; set; }
    public DateTime time { get; set; }
    public string text { get; set; }
  };

  private EditContext _editContext;
  private TimeSample _timeSample = new TimeSample();

  protected override void OnInitialized()
  {
    _editContext = new EditContext(_timeSample);
  }

  private void HandleValidSubmit()
  {
  }

  protected void HandleInvalidSubmit()
  {
  }

}

What browser are you using ?你使用的是什么浏览器 ? I ran your code with Chrome, and I could not reproduce the issue... I don't think it is related to Blazor, but rather to the browser you're using.我使用 Chrome 运行了您的代码,但无法重现该问题...我认为这与 Blazor 无关,而是与您使用的浏览器有关。 Please, try your code with other browser请用其他浏览器试试你的代码

hope this helps...希望这可以帮助...

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

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