I have a view with a form, when user submit the form, the matched action method is like:
public ActionResult Test(ViewModel vm, Member member)
{
//...
if (ModelState.IsValid)
{
try{
//...
member.OID = 1; //error here
//...
}Catch(Exception ex)
{
//...
}
}
}
It works fine before, but now I get error as below when assigning the value to a object property: Operation is not valid due to the current state of the object
why? how to resolve it?
From stackoverflow
-
It is not very clear as your code sample, if my guess is right. Member object is LINQ's class where you have OID as a FK to other object in your schema.
The error show that you cannot assign OID directly. say your O is Occupation Id. then you have to
member.Occupation = (from c in dc.Occupation where c.ID = 1 select c);Hope this helps.
KentZhou : Thanks. You are right. The OID is a FK. But If I assign member.Occupation = myOccupation; (myOccupation is a existing object match a existing row in db for table Occupation), and save it(SubmitChanges), it will give me another error said can not add duplicated key. It means it try to insert another row with same ID for Occupation. Quite confused.Jirapong : Your myOccupation object should load from same dataContext as member object loaded from. see - http://stackoverflow.com/questions/795196/linq-problem-with-inserting-new-rows-that-have-references-to-existing-records/795430#795430
0 comments:
Post a Comment