Reputation: 8359
I am working to add a edit function to my MVC 3 EF model first project, There is a list and a user is suppose to be able to edit a row of Questions
.
My entities are Questions
, CoreVaue
and SubjectType, CoreValue
and SubjectType
have many to many relationship to Question
.
Note: Dont make the name of CreateViewModel confuse you, I use it on Create and Delete views. :)
This is my GET action inside my Controller
:
public ActionResult Edit(int id)
{
Question q = Arep.GetQuestionById(id);
CreateViewModel model = new CreateViewModel();
List<SubjectType> subjectypes = Arep.getallS();
List<CoreValue> corevalues = Arep.getallC();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
model.CoreValues = new SelectList(corevalues, "CID", "Cname");
return View(model);
}
This is my methods inside my AdminRepository
:
public Question GetQuestionById(int id)
{
return db.Question.SingleOrDefault(m => m.QID == id);
}
public void changequestion(Question question)
{
db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
}
public List<SubjectType> getallS()
{
var Allsubjectypes = from SID in db.SubjectType
select SID;
return Allsubjectypes.ToList();
}
public List<CoreValue> getallC()
{
var AllCorevalues = from CID in db.CoreValue
select CID;
return AllCorevalues.ToList();
}
This is my POST action inside my Controller
:
[HttpPost, ActionName("Edit")]
public ActionResult EditConfirmed(CreateViewModel model)
{
Question question = new Question();
//question.QID = id;//test
if (ModelState.IsValid)
{
Arep.changequestion(question);
Arep.save();
return RedirectToAction("Edit");
}
var CoreValueID = int.Parse(model.Cname);
var SubjectTypeID = int.Parse(model.Sname);
var getallC = Arep.getbycid(CoreValueID);
var getallS = Arep.getbysid(SubjectTypeID);
return View(model);
}
And this is my CreateViwModel:
public string QuestionText { get; set; }
public string Cname { get; set; }
public string Sname { get; set; }
public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }
This is basicly my code for editing and I get this eror when I try to change values and submit it:
Current Object State Manager contains no Object Stat Entry that references an object of type NKI3.Models.Question.
I have no clue what casuses this error? :S
Thanks in advance!
Upvotes: 0
Views: 291
Reputation: 18181
In EditConfirmed, I think you want to get an existing question, not create a new question. The new question is not tracked by ObjectContext.
[HttpPost, ActionName("Edit")]
public ActionResult EditConfirmed(CreateViewModel model)
{
Question question = new Question(); // I think you want to get a existing question here
//question.QID = id;//test
if (ModelState.IsValid)
{
Arep.changequestion(question);
Arep.save();
return RedirectToAction("Edit");
}
var CoreValueID = int.Parse(model.Cname);
var SubjectTypeID = int.Parse(model.Sname);
var getallC = Arep.getbycid(CoreValueID);
var getallS = Arep.getbysid(SubjectTypeID);
return View(model);
}
Upvotes: 1