The data is not displayed in the edit.cshtml view

-2

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

    
asked by DC-Rom 05.11.2018 в 21:14
source

1 answer

0

Eye: you have some errors in the code improve it to be able to help you in the answer.

First: you can not do this

       @Html.TextBoxFor(model => model.GRADOESTUDIO, new { @class = "form-control" })

The problem is that GRADOESTUDIO is not a property of your user model, remember that the model you pass in sight is a user. I advise you to cast the viewbag, so that the razor knows that you are passing data from an entity or a model.

    
answered by 10.11.2018 в 01:11