I have the following templates:
public class Local {
[Key]
public int Id { get; set; }
public virtual Grupo Grupo { get; set; }
}
public class Grupo
{
[Key]
public int Id { get; set; }
[Required]
public int? LocalId { get; set; }
[Required]
[StringLength(60)]
public string Nome { get; set; }
[ForeignKey("LocalId")]
public virtual Local Local { get; set; }
}
In my controller before validating the model and adding it to the database I report the id
of Local
through a session .
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult _Create(Grupo model)
{
model.LocalId = SessionContext.LocalSelecionado;
if (ModelState.IsValid)
{
_service.Add(model);
return RedirectToAction("_List");
}
return PartialView(model);
}
Every time the validation fails, saying LocalId
is required, and it is, but I'm putting the value in the breakpoint if I look at the locals of the model is there value and yet fails.
The only way I was able to do this is by typing a field named LocalId
and bringing the value of the form so it still recognizes and validation does not fail.
What can it be?