Error executing SaveChanges ()

0

I'm doing a ActionResult that helps me to do a update and to do a insert , depending on the flag that passes as a parameter, this I had already done with other controllers and it works perfectly, but with this it sends an error to me when executing the SaveChanges() method, already it reviews my model and if the tables of my BD have been updated but everything is well. generate a controller with scaffolding and edit me perfectly but with mine no.

My ActionResult , the SaveChanges after the else is what makes the update and I mark the error

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CrEd([Bind(Include = "IdUsuario,Nombre,Contrasena,Descripcion,Estatus,IdRol")] Usuarios usuarios, int bandera)
    {
        if(bandera == 1)
        {
            if (ModelState.IsValid)
            {
                db.Usuarios.Add(usuarios);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IdRol = new SelectList(db.UsuariosRol, "IdRol", "Nombre", usuarios.IdRol);
            return View(usuarios);
        }else
        {
            if (ModelState.IsValid)
            {
                db.Entry(usuarios).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdRol = new SelectList(db.UsuariosRol, "IdRol", "Nombre", usuarios.IdRol);
            return View(usuarios);
        }
    }

my eyesight

 @model BackBolsaDeTrabajo.Data.Usuarios

  @{
     ViewBag.Title = "CrEd";
     string bandera = Request.QueryString["bandera"];

 }

 <h2>CrEd</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Usuarios</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">

          <input type="hidden" value="@Model.IdUsuario" name="IdUsuario" />
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
            </div>
        </div>

        <input type="hidden" value="@bandera" name="bandera" />

        <div class="form-group">
            @Html.LabelFor(model => model.Contrasena, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Contrasena, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Contrasena, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Descripcion, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Estatus, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Estatus)
                    @Html.ValidationMessageFor(model => model.Estatus, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IdRol, "IdRol", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("IdRol", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.IdRol, "", new { @class = "text-danger" })
            </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")
}

The error

    
asked by Roberto Dominguez 24.07.2017 в 18:50
source

1 answer

2

This error is common when you do not pass a correct clave , ie key 0 by default to contexto to save, update or delete data. To correct, you must ensure that in your view you have the field hidden with id correctly in the following way.

@Html.HiddenFor(model => model.IdUsuario)
    
answered by 24.07.2017 / 19:45
source