2
Following the models that of that question, I am having problems trying to save the data (keys of each of the records) in the table created to make the data relationship. Follows the post method I built:
Controller
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Novo(UsuariosNovo form, NivelCheckbox checkbox)
{
var usr = new Usuario();
foreach (var nivelId in form.Niveis)
{
var item = new NivelUsuario() { nivelid = checkbox.nivelid, usuarioid = form.usuarioid };
db.NivelUsuario.Add(item);
}
if (db.Usuario.Any(u => u.nome == form.nome))
ModelState.AddModelError("nome", "O nome do usuario precisa ser unico.");
if (!ModelState.IsValid)
return View(form);
usr.descricao = form.descricao;
usr.nome = form.nome;
usr.SetPassword(form.senha);
db.Usuario.Add(usr);
db.SaveChanges();
return RedirectToAction("Index");
}
Viewmodel
public class NivelCheckbox
{
public int nivelid { get; set; }
public bool IsChecked { get; set; }
public string nome { get; set; }
}
public class UsuariosNovo
{
public List<NivelCheckbox> Niveis { get; set; }
[HiddenInput]
public int usuarioid { get; set; }
[Required(ErrorMessage = "Obrigatório informar a Descrição")]
[Display(Name = "Descrição")]
public string descricao { get; set; }
[Required(ErrorMessage = "Obrigatório informar o Login")]
[Display(Name = "Login")]
public string nome { get; set; }
[Required(ErrorMessage = "Obrigatório informar a Senha")]
[Display(Name = "Senha"), DataType(DataType.Password)]
public string senha { get; set; }
}
View
<div class="panel panel-default col-sm-10 col-sm-offset-2">
<div class="panel-heading">Niveis</div>
<div class="panel-body">
<ul class="list-group">
@for (var i = 0; i < Model.Niveis.Count; i++)
{
<li class="list-group-item">
@Html.Hidden("Niveis[" + i + "].nivelid", Model.Niveis[i].nivelid)
<label for="Niveis_@(i)__IsChecked">
@Html.CheckBox("Niveis[" + i + "].IsChecked", Model.Niveis[i].IsChecked)
@Model.Niveis[i].nome
</label>
</li>
}
</ul>
</div>
</div>
When this action is called an exception is invoked:
23503: insert or update in table "nivel_usuarios" viola foreign key constraint "nivel_usuarios_nivelid_fkey"
By the debug I did, the id’s come reset to the controller, does anyone know where I’m going wrong? Or is there a more correct way to do this? (Pass level id and user to another table)
Could you put in your question how is the Form also?
– Leonel Sanches da Silva
I put the part of the levels... I know the conditions are missing to only add in the bank if the Ischecked is true and tals...
– Matheus Silva
I didn’t understand this variable
NivelCheckbox
. Ascheckbox
you need are no longer inUsuarioNovo
?– Leonel Sanches da Silva
You talk about Controller, right? Really, I was going too far, it was not necessary to do this, I passed this because I was not able to catch the nivelid in the foreach loop... @Ciganomorrisonmendez
– Matheus Silva