Hide the form when a value is selected from the dropdown list
I have these forms.
<div class="form-group">
@Html.LabelFor(model => model.RoomsNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RoomsNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RoomsNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryId, "Type of home", htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("ProductCategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProductCategoryId, "", new { @class = "text-danger" })
</div>
</div>
I want when I select a specific value from the dropdown to hide the form using RoomNumber. I do not know how to do that.
+3
source to share
1 answer
You can use JavaScript easily, you need an ID for your room and another ID for the product category dropdown , you need to add an event listener to listen for your dropdown change event :
Js
var ddl = document.getElementById('ddlProductCategory'),
form = document.getElementById('roomsForm');
ddl.addEventListener('change', function(){
if (this.value === '5'){
form.style.display = 'none';
}
else {
form.style.display = 'block';
}
});
Html
<div class="form-group" id="roomsForm">
@Html.LabelFor(model => model.RoomsNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RoomsNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RoomsNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryId, "Type of home", htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("ProductCategoryId", null, htmlAttributes: new { @class = "form-control", id = 'ddlProductCategory" })
@Html.ValidationMessageFor(model => model.ProductCategoryId, "", new { @class = "text-danger" })
</div>
</div>
+1
source to share