3
Hello, I am not able to insert data in a table that has foreign key, follow my model:
2 tables, Departamento
and Funcionario
, the official has the foreign key IdDepartamento
.
And I ran the controllers
all neat, I can add a department without problems, but when I add an employee, he error, screen of registering run with the dropdown
of the departments:
ae when I put a name and id of the department already created, gives the following error:
the controller
employee:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IdFuncionario,Nome,IdDepartamento")] Funcionario funcionario)
{
if (ModelState.IsValid)
{
db.Funcionarios.Add(funcionario);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(funcionario);
}
Funcionarios Create Controller
to generate the ViewBag
:
// GET: Funcionarios/Create
public ActionResult Create()
{
ViewBag.IdDepartamento = new SelectList(db.Departamentos, "IdDepartamento", "Nome");
return View();
}
View Create
of the Staff:
@model MVC4.Models.Functional
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Funcionario</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IdDepartamento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.IdDepartamento, "", new { @class = "text-danger" })
@Html.DropDownList("IdDepartamento", ViewBag.IdDepartamento as SelectList, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Thank you
RESOLVED
Guys, thanks to everyone who helped me, I was able to solve it using the following code on FuncionariosController
:
var dp = db.Departamentos.Find(Convert.ToInt32(funcionario.IdDepartamento));
if (dp == null)
{
dp = new Departamento();
}
funcionario.Departamento = dp;
That is, he gives a find
in departments to see if it meets the past Iddepartment, if it is == null, dp gets the Department class.
The error described has nothing to do with error in the foreign key, it is just the opposite, the foreign key is generating the error why it is working, and warning that the department table has nothing. idDepartment = 1, does not exist.
– Christian Beregula
We also need your code View.
– Leonel Sanches da Silva
@Ciganomorrisonmendez I will post the View do create
– Furlan
@Ciganomorrisonmendez has table data Departments
– Furlan
I agree with @Christianberegula, this error occurs because your employee table is referencing to a table that does not give you the expected record. Very common error... Can be simulated in SQL. The idea is that this field "Iddepartamento", is a combobox with the list of departments (Register the departments before the employee), which will work well.
– André Monteiro
@Ciganomorrisonmendez I made some changes to the question, please take a look
– Furlan