Note that there are some explanatory texts on larger screens.

plurals
  1. POmvc4 Edit function nothing happens
    text
    copied!<p>I have problem with edit in mvc4. In controller I added this code:</p> <pre><code>public ActionResult Edit(int id) { using (var db = new MainDatabaseEntities()) { var data = db.ANIME.Where(s =&gt; s.ID_AN == id).Select(c =&gt; new { ID_AN = c.ID_AN, TITLE_OR = c.TITLE_OR, TITLE_EN = c.TITLE_EN, GENRES = c.GENRES}).ToList().Select(s =&gt; new { ViewModel = new ANIME { ID_AN = s.ID_AN, TITLE_OR = s.TITLE_OR, TITLE_EN = s.TITLE_EN, }, GENREID = s.GENRES.Select(c =&gt; c.ID_GE), }).SingleOrDefault(); if (data == null) return HttpNotFound(); data.ViewModel.GENRES = db.GENRES.Select(c =&gt; new { ID_GE = c.ID_GE, GENRE = c.GENRE}).ToList().Select(c =&gt; new GENRES { ID_GE = c.ID_GE, GENRE = c.GENRE }).ToList(); foreach (var c in data.ViewModel.GENRES) c.isSelected = data.GENREID.Contains(c.ID_GE); return View(data.ViewModel); } } [HttpPost] public ActionResult Edit(ANIME anime) { var db = new MainDatabaseEntities(); var editanime = db.ANIME.Include("GENRES").FirstOrDefault(a =&gt; a.ID_AN == anime.ID_AN); if (editanime == null) { editanime.GENRES = anime.GENRES; } foreach (var genre in anime.GENRES) { if (genre.isSelected) { if (!editanime.GENRES.Any(c =&gt; c.ID_GE == genre.ID_GE)) { var addedgenre = new GENRES { ID_GE = genre.ID_GE }; db.GENRES.Attach(addedgenre); editanime.GENRES.Add(addedgenre); } } else { var removedgenre = editanime.GENRES.SingleOrDefault(c =&gt; c.ID_GE == genre.ID_GE); if (removedgenre != null) editanime.GENRES.Remove(removedgenre); } } } db.SaveChanges(); return RedirectToAction("Index"); } </code></pre> <p>When I edit genres with checkboxes it saves but if I change TITLE_OR or TITLE_EN nothing happens. I tried to add <code>db.Entry(anime).State = EntityState.Modified;</code> but then I get error: <code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</code> Any way to solve this?</p> <p>Edit: I still have problem with this ;_;. I tried to add AsNoTracking() but then genres won't save. Please help...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload