I have this
<div class="mb-3">
<label asp-for="DeliveryDt" class="control-label"></label>
<input asp-for="DeliveryDt" type="text" class="datepicker form-control" autocomplete="off" autofocus="true" readonly />
<span asp-validation-for="DeliveryDt" class="text-danger"></span>
</div>
i want to hide this field from my razor view.but i have no idea. In Asp.net simple we Visible = 'False' ,but this is not working in .Net core 6 MVC please show me idea except $('#DeliveryDt').hide();
I have tried this $('#DeliveryDt').hide(); this is working but it only hides textbox ,label is still dispalying as it is.
please share some idea except jquery or javascript.
You can use css display: none
:
<div class="mb-3" style="display:none">
<label asp-for="DeliveryDt" class="control-label"></label>
<input asp-for="DeliveryDt" type="text" class="datepicker form-control" autocomplete="off" autofocus="true" readonly />
<span asp-validation-for="DeliveryDt" class="text-danger"></span>
</div>
If you want to optional control the div visible by backend, you could define a property in your model.
Model
public class YourViewModel
{
public DateTime DeliveryDt { get; set; }
public bool ShowDeliveryDt { get; set; } = true; // Default to true, set to false to hide
}
View
use if statement
@if (Model.ShowDeliveryDt)
{
<div class="mb-3">
<label asp-for="DeliveryDt" class="control-label"></label>
<input asp-for="DeliveryDt" type="text" class="datepicker form-control" autocomplete="off" autofocus="true" readonly />
<span asp-validation-for="DeliveryDt" class="text-danger"></span>
</div>
}
Or you can use css
<div class="mb-3 @(Model.ShowDeliveryDt ? "" : "hidden-field")">
<label asp-for="DeliveryDt" class="control-label"></label>
<input asp-for="DeliveryDt" type="text" class="datepicker form-control" autocomplete="off" autofocus="true" readonly />
<span asp-validation-for="DeliveryDt" class="text-danger"></span>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<style>
.hidden-field {
display: none;
}
</style>
}
Controller
public IActionResult YourActionMethod()
{
var model = new YourViewModel
{
// Set other properties as necessary
ShowDeliveryDt = false // Set to false to hide the field
};
return View(model);
}