0

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.

Jooooosh
  • 331
  • 2
  • 4
  • 13
  • I'm afraid to ask but is `MyModel` an entity type? – Guru Stron Dec 11 '14 at 17:15
  • 2
    If you want it to be treated as a new entity, would it not be best to do precisely that and _create_ a new entity (or a model representation thereof)? – markpsmith Dec 11 '14 at 17:19
  • Yeah I replaced my entity name with that. I probably should have chose a better name. – Jooooosh Dec 11 '14 at 17:24
  • @markpsmith Would that only be possible through a copy constructor? I'd rather avoid creating one if possible, but I will if I have to. – Jooooosh Dec 11 '14 at 17:29
  • @Jooooosh, what is a problem with copy constructor? If it is an entity type you can try to play with it's `EntityState` or detach it from context(what can also be achieved via playing with state=), but it sounds like something very nasty=) – Guru Stron Dec 11 '14 at 17:34
  • @markpsmith There's a lot of attributes, so I wanted to try and avoid making one. Playing with the state does seem like a bad idea. I should probably make one, but I'll mess around with the state and see what happens before then. Thanks for the help. I just realized sublime would help with all those properties, so I'll use that. – Jooooosh Dec 11 '14 at 17:47
  • 1
    @Jooooosh the copy constructor can be defined using reflection/reflection+expressions and you will not need to manually work with props or add/delete them in you constructor when class is changed – Guru Stron Dec 11 '14 at 17:55
  • That looks way easier. Just gotta find out how to exclude specific fields. My date formats get messed up too, but that's probably for another question. – Jooooosh Dec 11 '14 at 18:29
  • @GuruStron It doesn't seem to be working. I have my copy constructor, and copy it with an id of 0. I follow it in the debugger to the view and see that the model's id is 0, but on the finished page I view the source and see the model's id has been set back to its previous value, which it never should have been. Any thoughts? I'm lost here. – Jooooosh Dec 11 '14 at 20:35
  • @Jooooosh gotta see the code=) – Guru Stron Dec 11 '14 at 21:47
  • This question's kind of old, but this answer worked for me in case anyone else has this problem. http://stackoverflow.com/a/27507040/4051272 – Jooooosh Jan 16 '15 at 13:42

0 Answers0