I have these 2 actions on a controller
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddComDefeito(PecaRegisto pecaRegisto)
{
if (!ModelState.IsValid)
{
return PartialView("_AddComDefeitoPartial", pecaRegisto);
}
return PartialView("_AddComDefeitoPartial", new PecaRegisto());
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddSemDefeito(PecaRegisto pecaRegisto)
{
if (!ModelState.IsValid)
{
return PartialView("_AddSemDefeitoPartial", pecaRegisto);
}
return PartialView("_AddSemDefeitoPartial", new PecaRegisto());
}
this is the model
public class PecaRegisto
{
public int Id { get; set; }
[Required]
[Range(1, int.MaxValue)]
public int Quantidade { get; set; }
[Required]
[Display(Name = "ReferĂȘncia")]
public int ReferenciaId { get; set; }
public Referencia Referencia { get; set; }
[Required]
[Display(Name = "Defeito")]
public int? DefeitoCodigoId { get; set; }
public DefeitoCodigo DefeitoCodigo { get; set; }
public int ProducaoRegistoId { get; set; }
public ProducaoRegisto ProducaoRegisto { get; set; }
}
In one of my actions the property
DefeitoCodigoId
Is not needed and i don't even render it on the partial. However the validation fails when checking modelstate.
So my only choices here are.
Automatically set the property valid within the action, or remove the Required and make the property required on the other action on the code.
I would rather just set the property valid since it's an optional foreign key.
How can i do this though?