如何使用C#属性绑定输入类型时间,并在更改更改后的获取值

发布于 2025-01-31 12:11:46 字数 293 浏览 2 评论 0 原文

当我与C#属性绑定时,我无法获取值或更改输入类型时间的值。

<input type="time" class="form-control" @bind="EndTime" @bind:format="HH:mm">

c#属性

public DateTime EndTime { get; set; }

如果末日已经包含值,则如何绑定该属性,那么它将显示该属性,如果值更改,则末端时间值将会更改。

I am not able to fetch the value or change the value of input type time when I am binding with the c# property.

<input type="time" class="form-control" @bind="EndTime" @bind:format="HH:mm">

C#Property

public DateTime EndTime { get; set; }

How to bind that property if the EndTime already contains value then it will show that and if the Value change then the EndTime value will change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2025-02-07 12:11:46

当您设置输入值以说“ 12:30”时,这不再是有效的日期格式(没有日期!),因此Blazor无法将其保存回 Endtime 。您将其设置为,大利士试图更新字段并失败,并且该值将设置回当前有效值,

如果您只需要有一个时间属性,则使用 timeonly 并将输入类型设置为时间,它将正常工作。

请记住,日期/时间的实际格式由浏览器和语言环境设置控制。

这是我的测试页面:

@page "/"

<h3>Date Time Testing</h3>

<div class="m-2">
    <input class="form-control" type="time" @bind=model.Time />
</div>
<div class="m-2">
    <input class="form-control" type="date" @bind=model.Date />
</div>
<div class="m-2">
    <input class="form-control" type="time" @bind=model.DateAndTime @bind:format="hh:mm" />
</div>
<div class="m-2">
    Time : @model.Time.ToLongTimeString()
</div>
<div class="m-2">
    Date : @model.Date.ToLongDateString()
</div>
<div class="m-2">
    DateTime : @model.DateAndTime.ToString()
</div>
@code {
    private ModelData model = new ModelData();

    public class ModelData
    {
        public TimeOnly Time { get; set; }
        public DateTime DateAndTime { get; set; }
        public DateOnly Date { get; set; }
    }
}

When you set the input value to say "12:30", this is no longer a valid date format (there's no date!), so Blazor can't save it back to EndTime. You set it, Blazor tries to update the field and fails, and the value is set back to the current valid value,

If you only want a Time property then use TimeOnly and set the input type to time and it will work correctly.

Remember, the actual formatting of Date/Time is controlled by the browser and the locale settings.

Here's my test page:

@page "/"

<h3>Date Time Testing</h3>

<div class="m-2">
    <input class="form-control" type="time" @bind=model.Time />
</div>
<div class="m-2">
    <input class="form-control" type="date" @bind=model.Date />
</div>
<div class="m-2">
    <input class="form-control" type="time" @bind=model.DateAndTime @bind:format="hh:mm" />
</div>
<div class="m-2">
    Time : @model.Time.ToLongTimeString()
</div>
<div class="m-2">
    Date : @model.Date.ToLongDateString()
</div>
<div class="m-2">
    DateTime : @model.DateAndTime.ToString()
</div>
@code {
    private ModelData model = new ModelData();

    public class ModelData
    {
        public TimeOnly Time { get; set; }
        public DateTime DateAndTime { get; set; }
        public DateOnly Date { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文