I have an edit/create page for a model. When the user submits the form, it'll be added or updated to the database.
After that happens, I want to redirect them back to the same form while keeping the same data, with a cleared out id as well as some other values.
public ActionResult AddProduct(MyModel myModel)
{
// Save
if (myModel.AuditID != 0) {
Update(myModel);
} else {
Add(myModel);
}
// Set some values so it's seen as new, as well as some
// other values that need to be cleared
myModel.ID = 0;
myModel.Product = "";
// Edit/Create page
ActionResult ret = EditCreateKnownRow(myModel);
return ret;
}
I want it to be treated as a completely new entity, but I get an InvalidOperationException
with these details:
The property 'ID' is part of the object's key information and cannot be modified.
I get that entity framework doesn't want to deal with the foreign key constraints that may exist, but that has nothing to do with what I'm looking for. Is there a way to treat it as a new entity without having to create a copy constructor?
Thanks.
Would doing something to the ModelState help? I've tried ModelState.Remove(myModel.ID.ToString());
and ModelState.Clear();
before modifying the key, but it didn't work.