0
I have the following script:
@Scripts.Render("~/bundles/jquery")
<script>
$(document).ready(function () {
$('.ocultar').hide();
$('#dn').change(function () {
if ($("#dn").attr("checked",true)) {
$('.ocultar').show();
}
else {
$('.ocultar').hide();
}
});
});
<div id="dn">
@Html.CheckBoxFor(x => x.DN) <b>Dpto. Nacional </b>
</div>
<div><br /></div>
<div class="ocultar">
<b> Número do Ofício </b>
</div>
<div class="ocultar">
@Html.EditorFor(x => x.Numero)
@Html.ValidationMessageFor(x => x.Numero)
</div>
<div class="ocultar">
<b> Observacao </b>
</div>
<div class="ocultar">
@Html.TextAreaFor(x => x.Observacao)
</div>
The script works fine. Load the page with the fields in hidden and if I check the checkbox 'DN' the fields 'Number' and 'Note' appear. But I have two small problems: If I uncheck the checkbox the two fields do not disappear. And if I leave marked and try to save, when clicking the save button, the two fields disappear and the message that the field is required that should appear, does not appear. That is, when saved and does not pass, load the 'Hide' again. How to correct these two instabilities?
Got @bfavaretto. I made the change to 'prop' but when testing and clicking the checkbox nothing happens. Hidden fields do not appear. Something else is missing?
– PFVictor
Now that I realize
#dn
is a div. Try to change to$('#dn input').change...
(swap all)– bfavaretto
Ok @bfavaretto , solves one of the problems that is: if I mark the fields appear, if I clear the fields disappear. Beauty. But continues the other. If I try to save without filling the field 'Number' the fields disappear, so the mandatory message also in hidden understands?
– PFVictor
In fact now the question becomes the $('.hide'). Id(); When I click on save but does not change the form due to validation failure, it is as if I am entering the form for the first time, that is, load the 'Hide'. How to understand that this is not the first load of the screen, but that it was validation failure and the fields should remain visible and with the apparent error message? @bfavaretto
– PFVictor
@Pfvictor I think this is worth a separate question, it’s another problem.
– bfavaretto