1
I got 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 complete the Registration and 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 do so in case the checkbox is marked the fields are mandatory and when it is not marked the fields remain hidden and without validation?
Sorry @Ciganomorrisonmendez, I did not understand very well. The fields that only need to be mandatory if DN is marked is session and enrollment. I will edit the question with the class of my entire model because I believe I cannot change it to Ivalidatableobject
– PFVictor
Why can’t you?
– Leonel Sanches da Silva
Oh yes... it worked @Gypsy. Perfect!
– PFVictor