Dylan
Dylan

Reputation: 1118

Input Date binding

I'm trying to bind to :

public DateTime? DOB { get; set; }

This works perfect:

<input type="date" format-value="MM/dd/yyyy" @bind="MyObject.DOB">

What I need to work is,

<input type="date" format-value="MM/dd/yyyy" value="@MyObject.DOB"  @onchange="@( (args) => { MyObject.DOB = args.Value.ToString().TryDateConvert(); MyMethod();})">

Or

<input type="date" format-value="MM/dd/yyyy" value="@MyObject.DOB"  @onchange="@( (args) => DOBChanged(args))">

&

public void DOBChanged(ChangeEventArgs e)
{
    var myVal =e.Value.ToString();
    MyObject.DOB = myVal.TryDateConvert();
    MyMethod();
}

I've tried a few variations, but no matter what it will bind to the MyObject.DOB, but it will not display in the textbox.

It will reset back to MM/DD/YYYY. I've used this way on other controls, but for some reason the date is throwing it for a loop.

Thanks.

Upvotes: 0

Views: 1254

Answers (1)

Mofaggol Hoshen
Mofaggol Hoshen

Reputation: 738

<input type="date"> is not really working well with format-value attributes you can read more about it https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date.

You can try this -

<input type="date" value="@MyObject.DOB.ToString("yyyy-MM-dd")" @onchange="@( (args) => DOBChanged(args))">

public void DOBChanged(ChangeEventArgs e)
    {
        var myVal = e.Value.ToString();
        MyObject.DOB = DateTime.Parse(myVal);
        MyMethod();
    }

Upvotes: 1

Related Questions