I have the following model:
public class Request : IEntity
{
public int Id { get; set; }
[Required(ErrorMessage = "O Centro de Responsabilidade é obrigatório!")]
public string Cadastro_Id { get; set; }
public virtual Cadastro Cadastro { get; set; }
[Required(ErrorMessage = "A requisição é obrigatória!")]
public string Nome { get; set; }
public bool DN { get; set; }
public string Matricula { get; set; }
public string Sessao { get; set; }
object IEntity.Id
{
get { return Id; }
set { Id = (int)value; }
}
}
All fields automatically loaded when entering the page are required. In this form there is a checkbox (DN) that if marked, makes it mandatory to fill in the Enrollment and the Session. If DN is not marked, these two fields do not appear and are not mandatory (use in script to hide the fields).
Follow my view:
<table>
<tr>
<td>@Html.DropDownList("Cadastro_Id", string.Empty)</td>
<td>@Html.DropDownList("Nome", string.Empty)</td>
</tr>
</table>
<br />
<div id="dn">
@Html.CheckBoxFor(x => x.DN) <b> NACIONAL </b>
</div>
<div><br /></div>
<div class="ocultar">
<b> Número de Matrícula </b>
</div>
<div class="ocultar">
@Html.EditorFor(x => x.Matricula)
</div>
<div class="ocultar">
<b> Sessão </b>
</div>
<div class="ocultar">
@Html.TextAreaFor(x => x.Sessao)
</div>
How to make checkboxes checked if the fields are mandatory and when not checked the fields remain hidden and without validation?