无法更改 Blazor 中绑定的输入类型时间

问题描述

您好,我遇到了一个问题。我需要编辑 html Table 上的日期时间值。我将表格数据作为字符串获取,然后转换为 Datetime 并在 Html 的时间字段中获取值我的意思是我可以获取该值但我无法从 The Html 进行编辑。这是我的代码

 <input type="time" style="width:100%"   @bind="@my_DateTime" />

这是我的模型类,请注意,我将数据作为字符串获取,然后将其转换为日期时间。

 private DateTime? _my_DateTime;
    [Parameter]
    public DateTime? my_DateTime
    {
        get
        {
            if (mytimestring != "")   //This is String I got data 
            {
                _my_DateTime = Convert.ToDateTime(mytimestring);
                return _my_DateTime ;
            }
            else
            {
                _my_DateTime  = null;
                return _my_DateTime ;
            }
        }
        set
        {
           

            
            _my_DateTime = value;
            OnPropertyChanged("my_DateTime");
            //}
            //  NotifyOfPropertyChange(() => 並び順);
        }
    }

这是我从表中得到的字符串属性

 private string _mytimestring;
    [Parameter]
    public string mytimestring 
    {
        get
        {
            return _mytimestring ;
        }
        set
        {
            _mytimestring  = value;
            OnPropertyChanged(mytimestring");
            //  NotifyOfPropertyChange(() => 並び順);
        }
    }

提前致谢。请不要我将 Value 绑定到像 这样的文本框,它可以工作,但只有 Datetime 我导致问题。请帮帮我

解决方法

我认为这是在互联网上已经有很多建议之后发生变化的事情之一。我发现绑定完美无缺,无需任何解析或格式化。

我也很惊讶地看到,当您将 html time 输入绑定到 C# DateTime 时,它实际上会自动填充今天的时间和日期。 (您的问题不需要,但我认为这是一件非常有趣的事情)

以下对我来说效果很好。

@page "/time"

<input type="time" @bind="@SomeTime" />
<br />
@SomeTime.ToString("hh:mm tt")
<br />
@SomeTime.ToString()

@code {
    public DateTime SomeTime = new DateTime();
}