Josh
Josh

Reputation: 685

Is there a way to create a read-only date box in blazor without using two-way binding?

            <input type="date" @bind-value="masterEditItem.Date" class="form-control col-md-4" readonly="readonly" />
            <input type="date" class="form-control col-md-4" readonly="readonly" value="@masterEditItem.Date.ToString("yyyy-mm-dd")" />
            <input type="date" class="form-control col-md-4" value="@masterEditItem.Date" />

The outputs of the above three lines are:

enter image description here

It seems that if I want a read-only date box then I will have to use two-way binding?

Is there a way to use one-way binding for a read-only date box? (because some of my date properties are read-only and they do not work with two-way binding, I also want to use the class="form-control" to look like a text box)

Thanks!

Upvotes: 1

Views: 2215

Answers (1)

enet
enet

Reputation: 45626

You can use an input type text as follows:

@page "/"

<input type="text"  readonly="readonly" value="@stringDate"  />

@code{

    public DateTime MyDate { get; } = DateTime.Now;
    private string stringDate;

        protected override void OnInitialized()
        {
            stringDate =  MyDate.ToShortDateString();

            base.OnInitialized();
        }

}

Upvotes: 4

Related Questions