Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind input of type time with blazor

Hello i have 2 variables of type int that i would like to bind to the min and max values of an input of type time.
How can i do this?

I do not know what to place in the bind field since there are 2 different variables. Also there is the min and max attributes.

<input type="time" min="@model.min" max="@model.max" bind=?/>

What should i put in the bind ?

Update On a more thoroughly analysis i decided i will need 2 variables of type Timespan and i will bind these to 2 inputs of type time.

like image 747
Bercovici Adrian Avatar asked Feb 25 '19 14:02

Bercovici Adrian


2 Answers

You cannot bind a TimeSpan directly to an input in Blazor, but you can use a property to convert it to/from a string.

<input type="time" min="@model.min" max="@model.max" bind="@TimeProxy" />

and

@functions
{
  string TimeProxy { get => model.Time.ToString(); set => TimeSpan.TryParse(value,out model.Time); }
}
like image 127
Mister Magoo Avatar answered Sep 27 '22 23:09

Mister Magoo


Previous solution was not working for me with .net Core 3.1 so I'll add an updated one:

Use Blazor :

<EditForm Model=@model OnValidSubmit="Submit">
    <InputText type="time" @bind-Value="TimeProxy" />
</EditForm>

Code changes are necessary as well.

@code {
    // This field is required as you can not use property in out statement
    private TimeSpan LocalTime = TimeSpan.FromHours(0);
    private string TimeProxy { 
        get => model.Time.ToString();
        set => TimeSpan.TryParse(value,out LocalTime);
    }
    private void Submit() {
        model.Time = LocalTime;
        // following submit logic...
    }
}
like image 40
Ivan Temchenko Avatar answered Sep 27 '22 21:09

Ivan Temchenko