I'm hitting a strange problem that I haven't been able to figure out. Here is a block of my markup.
<div class="col-md-4">
<div class="mb-3">
<label asp-for="OrderSearch.StartDate" class="control-label"></label>
<div class="input-group">
<button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" title="Predefined Dates">
<img src="~/images/mdi/calendar.svg" />
</button>
<ul class="dropdown-menu">
<li><a data-mode="1" href="javascript:void(0)" class="set-dates dropdown-item">Today</a></li>
<li><a data-mode="2" href="javascript:void(0)" class="set-dates dropdown-item">Yesterday</a></li>
<li><a data-mode="3" href="javascript:void(0)" class="set-dates dropdown-item">This Week</a></li>
<li><a data-mode="4" href="javascript:void(0)" class="set-dates dropdown-item">This Month</a></li>
<li><a data-mode="5" href="javascript:void(0)" class="set-dates dropdown-item">This Quarter</a></li>
<li><a data-mode="6" href="javascript:void(0)" class="set-dates dropdown-item">This Year</a></li>
<li><a data-mode="7" href="javascript:void(0)" class="set-dates dropdown-item">Last Week</a></li>
<li><a data-mode="8" href="javascript:void(0)" class="set-dates dropdown-item">Last Month</a></li>
<li><a data-mode="9" href="javascript:void(0)" class="set-dates dropdown-item">Last Quarter</a></li>
<li><a data-mode="10" href="javascript:void(0)" class="set-dates dropdown-item">Last Year</a></li>
</ul>
<input asp-for="OrderSearch.StartDate" class="form-control" />
</div>
<span asp-validation-for="OrderSearch.StartDate" class="text-danger"></span>
</div>
</div>
And here's how this markup gets rendered. Note that a new hidden element with the ID __Invariant
is added.
<div class="col-md-4">
<div class="mb-3">
<label class="control-label" for="OrderSearch_StartDate">Start Date (Order Closed)</label>
<div class="input-group">
<button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" title="Predefined Dates">
<img src="/images/mdi/calendar.svg" />
</button>
<ul class="dropdown-menu">
<li><a data-mode="1" href="javascript:void(0)" class="set-dates dropdown-item">Today</a></li>
<li><a data-mode="2" href="javascript:void(0)" class="set-dates dropdown-item">Yesterday</a></li>
<li><a data-mode="3" href="javascript:void(0)" class="set-dates dropdown-item">This Week</a></li>
<li><a data-mode="4" href="javascript:void(0)" class="set-dates dropdown-item">This Month</a></li>
<li><a data-mode="5" href="javascript:void(0)" class="set-dates dropdown-item">This Quarter</a></li>
<li><a data-mode="6" href="javascript:void(0)" class="set-dates dropdown-item">This Year</a></li>
<li><a data-mode="7" href="javascript:void(0)" class="set-dates dropdown-item">Last Week</a></li>
<li><a data-mode="8" href="javascript:void(0)" class="set-dates dropdown-item">Last Month</a></li>
<li><a data-mode="9" href="javascript:void(0)" class="set-dates dropdown-item">Last Quarter</a></li>
<li><a data-mode="10" href="javascript:void(0)" class="set-dates dropdown-item">Last Year</a></li>
</ul>
<input type="date" class="form-control" id="OrderSearch_StartDate" name="OrderSearch.StartDate" value="" />
<input name="__Invariant" type="hidden" value="OrderSearch.StartDate" />
</div>
<span class="text-danger field-validation-valid" data-valmsg-for="OrderSearch.StartDate" data-valmsg-replace="true"></span>
</div>
</div>
I'm using Bootstrap 5. Because my <input>
element is no longer the last element in the <div>
, the Bootstrap styling of my input group is messed up. I do not know whether or not this issue is related to Bootstrap.
I Googled quite a bit but couldn't find anything on this. Has anyone seen this before?
Update:
This seems like it might be related to the <input>
tag helper. If I explicitly set the type of my StartDate
control to text
or datetime
, the hidden element is not added. But I need it to be a date.
Currently, I'm specifying a date on the property like this:
[Display(Name = "Start Date (Order Closed)")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
I'm hitting a strange problem that I haven't been able to figure out. Here is a block of my markup.
<div class="col-md-4">
<div class="mb-3">
<label asp-for="OrderSearch.StartDate" class="control-label"></label>
<div class="input-group">
<button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" title="Predefined Dates">
<img src="~/images/mdi/calendar.svg" />
</button>
<ul class="dropdown-menu">
<li><a data-mode="1" href="javascript:void(0)" class="set-dates dropdown-item">Today</a></li>
<li><a data-mode="2" href="javascript:void(0)" class="set-dates dropdown-item">Yesterday</a></li>
<li><a data-mode="3" href="javascript:void(0)" class="set-dates dropdown-item">This Week</a></li>
<li><a data-mode="4" href="javascript:void(0)" class="set-dates dropdown-item">This Month</a></li>
<li><a data-mode="5" href="javascript:void(0)" class="set-dates dropdown-item">This Quarter</a></li>
<li><a data-mode="6" href="javascript:void(0)" class="set-dates dropdown-item">This Year</a></li>
<li><a data-mode="7" href="javascript:void(0)" class="set-dates dropdown-item">Last Week</a></li>
<li><a data-mode="8" href="javascript:void(0)" class="set-dates dropdown-item">Last Month</a></li>
<li><a data-mode="9" href="javascript:void(0)" class="set-dates dropdown-item">Last Quarter</a></li>
<li><a data-mode="10" href="javascript:void(0)" class="set-dates dropdown-item">Last Year</a></li>
</ul>
<input asp-for="OrderSearch.StartDate" class="form-control" />
</div>
<span asp-validation-for="OrderSearch.StartDate" class="text-danger"></span>
</div>
</div>
And here's how this markup gets rendered. Note that a new hidden element with the ID __Invariant
is added.
<div class="col-md-4">
<div class="mb-3">
<label class="control-label" for="OrderSearch_StartDate">Start Date (Order Closed)</label>
<div class="input-group">
<button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" title="Predefined Dates">
<img src="/images/mdi/calendar.svg" />
</button>
<ul class="dropdown-menu">
<li><a data-mode="1" href="javascript:void(0)" class="set-dates dropdown-item">Today</a></li>
<li><a data-mode="2" href="javascript:void(0)" class="set-dates dropdown-item">Yesterday</a></li>
<li><a data-mode="3" href="javascript:void(0)" class="set-dates dropdown-item">This Week</a></li>
<li><a data-mode="4" href="javascript:void(0)" class="set-dates dropdown-item">This Month</a></li>
<li><a data-mode="5" href="javascript:void(0)" class="set-dates dropdown-item">This Quarter</a></li>
<li><a data-mode="6" href="javascript:void(0)" class="set-dates dropdown-item">This Year</a></li>
<li><a data-mode="7" href="javascript:void(0)" class="set-dates dropdown-item">Last Week</a></li>
<li><a data-mode="8" href="javascript:void(0)" class="set-dates dropdown-item">Last Month</a></li>
<li><a data-mode="9" href="javascript:void(0)" class="set-dates dropdown-item">Last Quarter</a></li>
<li><a data-mode="10" href="javascript:void(0)" class="set-dates dropdown-item">Last Year</a></li>
</ul>
<input type="date" class="form-control" id="OrderSearch_StartDate" name="OrderSearch.StartDate" value="" />
<input name="__Invariant" type="hidden" value="OrderSearch.StartDate" />
</div>
<span class="text-danger field-validation-valid" data-valmsg-for="OrderSearch.StartDate" data-valmsg-replace="true"></span>
</div>
</div>
I'm using Bootstrap 5. Because my <input>
element is no longer the last element in the <div>
, the Bootstrap styling of my input group is messed up. I do not know whether or not this issue is related to Bootstrap.
I Googled quite a bit but couldn't find anything on this. Has anyone seen this before?
Update:
This seems like it might be related to the <input>
tag helper. If I explicitly set the type of my StartDate
control to text
or datetime
, the hidden element is not added. But I need it to be a date.
Currently, I'm specifying a date on the property like this:
[Display(Name = "Start Date (Order Closed)")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
This issue is discussed in a GitHub post.
It's related to a change that was made around .NET 7 that causes the <input>
tag helper to add a hidden element to help deal with potential culture issues.
In most cases, there would be no adverse effects. But, here, Bootstrap 5 input groups only round the corners for the first and last elements in the group. Because the tag helpers inserted an additional element after mine, mine didn't get rounded corners, which messed up the formatting.
If you can do without this feature, it can be disabled using the following code.
builder.Services.AddRazorPages(options =>
{
// ...
})
.AddViewOptions(options =>
{
options.HtmlHelperOptions.FormInputRenderMode = FormInputRenderMode.AlwaysUseCurrentCulture;
});