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 choose certain value from dropdown to hide the form with RoomsNumber. I don't know how to do this.
You can do it easyly using JavaScript, you need an ID for your rooms form and another ID for the product category drop down list, then you need add an event listener to hear the change event of your drop down:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With