I'm trying to do an action with jquery when on button press it changes a value in that database.
My code:
The button:
<a class="fa fa-2x fa-check confirmAnswer" href="#" onclick="Confirm(@item.ID)"></a>
JS:
var Confirm = function (solutionID) {
$.ajax({
type: "POST",
url: "/Help/ConfirmSolution",
data: { id: solutionID },
success: function (response) {
alert("Survey Deleted");
location.reload();
},
})
}
Controller Action:
[HttpPost]
public JsonResult ConfirmSolution(int id)
{
HelpSolutionViewModel answer = Mapper.Map<HelpSolution, HelpSolutionViewModel>(unitOfWork.HelpSolutionRepository.GetByID(id));
answer.Selected = true;
HelpSolution helpsolution = Mapper.Map<HelpSolutionViewModel, HelpSolution>(answer);
unitOfWork.HelpSolutionRepository.Update(helpsolution);
unitOfWork.Save();
return Json(helpsolution, JsonRequestBehavior.AllowGet);
}
Repository methods:
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
The error:
Attaching an entity of type failed because another entity of the same type already has the same primary key valuelockquote
By the way, the other fixes on the similar questions I saw don't help, I tried loads.