简体   繁体   English

为什么 value="string-value" 与 input type="time" 一起工作,而不是 bind-value="string-value"

[英]Why does value="string-value" work with input type="time", but not bind-value="string-value"

I'm working on a simple website to try out some Blazor (I'm a rookie).我正在一个简单的网站上尝试一些 Blazor(我是菜鸟)。 I have created a binding with a string value "08:00" and bind i to a input field as shown:我创建了一个字符串值“08:00”的绑定,并将 i 绑定到一个输入字段,如下所示:

<input @bind-value="@StartValue" @bind-value:event="onchange" class="col-sm-1" type="time"/>
@code {
public string StartValue { get; set; } = "08:00";
}

This generates the error "cannot convert from 'string' to 'System.DateTime'".这会生成错误“无法从‘字符串’转换为‘System.DateTime’”。 However, when I remove my binding and create my input as following: it works fine.但是,当我删除绑定并按如下方式创建输入时:它工作正常。

<input value="08:00" class="col-sm-1" type="time"/>

Any idéeas why there's a difference?任何想法为什么有区别? It doesn't make sense to me to use a DateTime, I would agree if I could use a TimeSpan but that doesn't work either.使用 DateTime 对我来说没有意义,如果我可以使用 TimeSpan,我会同意,但这也不起作用。

With @bind-value , you are binding to a string-type, which won't work with type="time".使用@bind-value ,您将绑定到字符串类型,该类型不适用于 type="time"。

With value , you entered a correct type "time", and not a string.使用value ,您输入了正确的类型“时间”,而不是字符串。 When you would enter a string like 'foo', it will not work.当您输入像 'foo' 这样的字符串时,它将不起作用。

I tried input type time with TimeSpan and got the following compilation error: Error CS1503 Argument 1: cannot convert from 'System.TimeSpan' to 'System.DateTime'我尝试使用 TimeSpan 输入类型时间并得到以下编译错误: Error CS1503 Argument 1: cannot convert from 'System.TimeSpan' to 'System.DateTime'

Blazor maps certain types of <input /> to certain types of CLR types. Blazor 将某些类型的<input />映射到某些类型的 CLR 类型。 The reason for this could be to reduce unwanted type and conversion errors, miss behaviors (?).这样做的原因可能是为了减少不需要的类型和转换错误、遗漏行为 (?)。

In the official docs of Blazor this is mentionet in "pieces" like:在 Blazor 的官方文档中,这是在“片段”中提到的,例如:

When the component is rendered, the value of the input element comes from the CurrentValue property.呈现组件时,输入元素的值来自 CurrentValue 属性。 When the user types in the text box and changes element focus, the onchange event is fired and the CurrentValue property is set to the changed value.当用户在文本框中键入内容并更改元素焦点时,将触发 onchange 事件并将 CurrentValue 属性设置为更改后的值。 In reality, the code generation is more complex because @bind handles cases where type conversions are performed.实际上,代码生成更复杂,因为@bind 处理执行类型转换的情况。 In principle, @bind associates the current value of an expression with a value attribute and handles changes using the registered handler.原则上,@bind 将表达式的当前值与 value 属性相关联,并使用注册的处理程序处理更改。

Link to the doc 链接到文档

You could use DateTime and provide a formater like:您可以使用DateTime并提供如下格式的格式化程序:

<input @bind="StartDate" @bind:format="yyyy-MM-dd" />

@code {
    [Parameter]
    public DateTime StartDate { get; set; } = new DateTime(2020, 1, 1);
}

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

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