Razor Page Create Input Type Date Turns Into Type Text

Viewed 680

I have a model which is of DateTime type and want to create textbox for it. The default generated control's type is datetime-local. I want to change it to date only since I don't need the time. However, for some reason the type always turned to text type after loading is finished.

Here is my code

<div class="form-group">
  <label asp-for="Order.TanggalOrder" class="control-label">Tanggal Order</label>
  <input asp-for="Order.TanggalOrder" type="date" class="" />
  <input asp-for="Order.TanggalOrder" type="time" class="form-control" />
  <span asp-validation-for="Order.TanggalOrder" class="text-danger"></span>
</div>

input date turns into input text

As you can see, the browser rendered the date input correctly for a brief moment, then turned into text. The datepicker is still there, but the input validation (dd/mm/yyyy text and calendar icon ) is no longer there.

This happens only with date type, as I tried it with time or datetime and they're still rendered properly.

Any clue on what I have to do? I'm using the .net 5 mvc.

1 Answers

You can use separate date and time controls. In order for the input tag helper to present the correct controls, more configuration is needed:

Model:

[BindProperty, DataType(DataType.Date)]
public DateTime Date{ get; set; }

View:

DateTime:
<input class="form-control" asp-for="Date" />

Result: enter image description here

enter image description here

Related