Edit/Update - ASP.NET MVC

Asked

Viewed 184 times

0

Good afternoon! I’m trying to edit from "João Torres" to "João Torres Moreira", but appears the error message below.

Repository update, insert, or delete instruction affected an unexpected number of lines (0). Entities may have been modified or deleted after loading. Update your Objectstatemanager.

Follow the image below.

inserir a descrição da imagem aqui

My Medicoscontroller.Cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CadeMeuMedico.Models;
using System.Data.Entity;
using System.Data;

namespace CadeMeuMedico.Controllers
{
    public class MedicosController : Controller
    {
        private CadeMeuMedicoBDEntities db = new CadeMeuMedicoBDEntities();
        //
        // GET: /Medicos/

        public ActionResult Index()
        {
            var medicos = db.Medicos.Include(m => m.Cidades).Include(m => m.Especialidades).ToList();
            return View(medicos);
        }

        public ActionResult Adicionar()
        {
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome");
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome");
            return View();
        }

        [HttpPost]
        public ActionResult Adicionar(Medicos medicos)
        {
            if (ModelState.IsValid)
            {
                db.Medicos.Add(medicos);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);

        }

        [HttpGet]
        public ActionResult Editar(long id)
        {
            Medicos medicos = db.Medicos.Find(id);
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);
        }

        [HttpPost]
        public ActionResult Editar(Medicos medicos)
        {
            if (ModelState.IsValid)
            {
                db.Entry(medicos).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);
        }

        [HttpPost]
        public string Excluir(long id)
        {
            try
            {
                Medicos medicos = db.Medicos.Find(id);
                db.Medicos.Remove(medicos);
                db.SaveChanges();
                return Boolean.TrueString;
            }
            catch
            {
                return Boolean.FalseString;
            }
        }
    }
}
  • because you’re forcing the EntityState.Modified; the ideal would be you to make a query through the "id", change the attributes you find necessary and then execute the SaveChanges(). The input parameter of the edit method is not part of your context, it is only the deserialization of the post received by the view.

1 answer

0

There are some possibilities for this error to be happening...

1st - E.F is not identifying that there is a change in its entity, because it may be that it does not actually exist in the bank.

2nd - If the first possibility is discarded, you can first consult the entity, placing it in the context of E.F, change whatever comes from your view and then update the object.

By eliminating these two possibilities, I believe it solves the problem. One approach you can also use for testing is, the following:

if (ModelState.IsValid)
{
    Db.Entry(medico).State = medico.MedicoId == null ? 
    EntityState.Added : EntityState.Modified;

    db.SaveChanges();
    return RedirectToAction("Index");
}

If there is no Medicoid the object receives the Status added which causes the context of E.F to interpret that it is adding this entity, if not there it receives the Status Modified.

Through Debug you can also check if the entity is coming to the filled actualization correctly. If this does not solve, post also, as your entity is arriving, adding more details of the problem becomes easier to reach a resolution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.