Note that there are some explanatory texts on larger screens.

plurals
  1. POObjectStateManager error
    text
    copied!<p>I have controller post action</p> <pre><code>public ActionResult Demographics(string submitButton, DemographicsViewModel model) { switch (submitButton) { case "Home": return RedirectToAction("Index"); case "Next Page": using (ProposalRepository proposalRepository = new ProposalRepository()) { model.Proposal.UserID = proposalRepository.GetUserByName(MasterHelper.CurrentUsername).UserID; model.Proposal.CustomerID = proposalRepository.GetCustomerByName(model.CustomerName).CustomerID; if (model.Proposal.ProposalID != 0) { proposalRepository.Update(model.Proposal); } else { proposalRepository.AddProposal(model.Proposal); } proposalRepository.SaveChanges(); } return RedirectToAction("GRQuestions", model.Proposal); default: return View(); } } </code></pre> <p>When I try to update</p> <pre><code>public void Update(Proposal proposal) { mContext.Proposals.ApplyCurrentValues(proposal); } </code></pre> <p>throw error: <b>An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied. </b> I was looking for a solution and I found this:</p> <pre><code>public ActionResult Demographics(string submitButton, DemographicsViewModel model) { switch (submitButton) { case "Home": return RedirectToAction("Index"); case "Next Page": using (ProposalRepository proposalRepository = new ProposalRepository()) { Proposal proposal = proposalRepository.GetById(model.Proposal.ProposalID); model.Proposal.UserID = proposalRepository.GetUserByName(MasterHelper.CurrentUsername).UserID; model.Proposal.CustomerID = proposalRepository.GetCustomerByName(model.CustomerName).CustomerID; if (model.Proposal.ProposalID != 0) { proposalRepository.Update(model.Proposal); } else { proposalRepository.AddProposal(model.Proposal); } proposalRepository.SaveChanges(); } return RedirectToAction("GRQuestions", model.Proposal); default: return View(); } } </code></pre> <p>add only one row:</p> <pre><code>Proposal proposal = proposalRepository.GetById(model.Proposal.ProposalID); </code></pre> <p>It works well but I think this is a stupid decision and I believe there is a better way and an explanation???</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