Hi,
Im confused. I have a few rules for two models:
Validation for model Sistemas:
This is my View:
So, How can I exclude one of this validations? Or, how can i handle this situation?
Thank you in advance. Regards!
M.S.
Im confused. I have a few rules for two models:
Validation for model Sistemas:
public class SistemasMetadata : AbstractValidator<Sistemas>
{
public SistemasMetadata()
{
RuleFor(s => s.IdSistemaDiccionario)
.NotNull().WithMessage("* Requerido")
.GreaterThan(0).WithMessage("* Requerido")
.NotEmpty().WithMessage("* Requerido");
}
}
Validation for model SistemasDiccionario:public class SistemasDiccionarioMetadata : AbstractValidator<SistemasDiccionario>
{
public SistemasDiccionarioMetadata()
{
RuleFor(sd => sd.Nombre)
.NotNull().WithMessage("* Requerido")
.Must((sd, nombre) => NombreUnico(sd, nombre)).WithMessage("* Nombre existente")
.Length(1, 200).WithMessage("* Longitud máxima de 200 caracteres");
RuleFor(sd => sd.Descripcion)
.Length(0, 500).WithMessage("* Longitud máxima de 500 caracteres");
}
private bool NombreUnico(SistemasDiccionario _sd, string nombre)
{
//existing logic its ok...
}
}
And i have a ViewModel with properties that referencing a model: public class SistemasConfiguracionViewModel
{
public Sistemas Sistemas { get; set; }
public SistemasDiccionario SistemasDiccionario { get; set; }
//existing logic its ok
}
And then, i have a view with above view model. In this view i have a check-box to hide or show some part of view. In first part, i just want get a property of "Sistemas". In second part i just want get a property of "SistemasDiccionario".This is my View:
@model <<NameSpaceItsOk>>.ViewModels.SistemasConfiguracionViewModel
<form id="form" method="post" action="">
<!-- HIDE OR SHOW -->
<div id="divCrear">
<p>
@Html.LabelFor(model => model.Sistemas.IdSistemaDiccionario, "Diccionario")
@Html.TextBoxFor(model => model.Sistemas.IdSistemaDiccionario)
</p>
</div>
<!-- HIDE OR SHOW -->
<div id="divCrearNuevo" style="display: none;">
<p>
@Html.LabelFor(model => model.SistemasDiccionario.Nombre)
@Html.TextBoxFor(model => model.SistemasDiccionario.Nombre)
</p>
<p>
@Html.LabelFor(model => model.SistemasDiccionario.Descripcion, "Descripción")
@Html.TextBoxFor(model => model.SistemasDiccionario.Descripcion)
</p>
</div>
<p style="text-align: center;">
<input type="button" id="btnSubmit" class="boton" value="Crear" />
</p>
</form>
When i hide one of this part and send httppost to my controller, the ModelState is invalid, because both properties ( i.e. both models ) are validated.So, How can I exclude one of this validations? Or, how can i handle this situation?
Thank you in advance. Regards!
M.S.