Here is the code that I have made, but I can not make the data I want to edit appear, the data that is in the same table appears, but not the data that is in another table which is related to the main table, using the viewbag.
Controller:
public ActionResult Edit(decimal id = 0)
{
USUARIO usuario = db.USUARIO.Find(id);
if (datos == null)
{
return HttpNotFound();
}
var area = db.AREA.Where(x => x.FKUSUARIO == id);
foreach (var xarea in areas)
{
ViewBag.areas = xarea.AREA1;
}
var grado = db.GRADOEST.Where(z => z.FKUSUARIO == id);
foreach (var zgrado in grados)
{
ViewBag.grados = zgrado.GRADOESTUDIO;
}
return View(usuario);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(USUARIO usuario)
{
if (ModelState.IsValid)
{
db.Entry(usuario).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(usuario);
}
Edit view:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.AREA1, "AREA")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.AREA1, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AREA1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GRADOESTUDIO, "GRADO DE ESTUDIO")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.GRADOESTUDIO, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.GRADOESTUDIO)
</div>
<footer>
<button type="submit" class="btn btn-sm btn-primary">
ACTUALIZAR DATOS
</button>
</footer>
}
THANK YOU IN ADVANCE