-1

I have a plans list, or whatever it is, and I don't want any of them to be deleted. That's why when someone choose the "edit" option - I actually want to add a new plan with the same references, but just a new ID. Then, I don't care at all what changes will be made in it in the edit view (which is in fact the create view).

I use the same view whether it create new or edit, but the only difference is that if the action get a plan - I understand it is not create new but edit and then I want to display in the create view all the "edited" plan parameters, and if there isn't any plan (if the action does not get any plan) - I understand it's a totaly new plan (someone choose the "Create new" option), and then I want to display the same view - with blank fields.

Here is my code:

public ActionResult CreatePlan(Plan? plan)
        {
            if (plan == null)
            {
                return View();
            }
            else 
            {
                Plan oldPlan = db.PlanSet.Single(p => p.Id == plan.Value.Id);
                return View(oldPlan);
            }
        }

Currently, as you can see, if the action does get an object - it lets me edit the old plan.

How can I duplicate it so any change that will be made in the view - will be saved with another plan ID??? Hope I made myself clear and am happy to get some help !

razlebe
  • 7,134
  • 6
  • 42
  • 57
ParPar
  • 7,355
  • 7
  • 43
  • 56

3 Answers3

0

I think what you want is: Object.MemberwiseClone().

Object.MemberwiseClone() creates a shallow copy of an object, i.e. it creates a new object and copies the references from the old object (of course, value types are duplicated).

Now, since MemberwiseClone is actually protected, you have to do something like:

public class Plan 
{
    public Plan clone()
    {
        return (Plan)this.MemberwiseClone();
    }
}
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Thanks, I tried to do what you are suggesting, but when it runs it's throwing an error: "The method or operation is not implemented". Do you know what it maens? – ParPar Nov 22 '11 at 12:26
  • Very strange. Where are you getting the error? I just tested this with some dummy classes and it works ok. – Tudor Nov 22 '11 at 12:30
  • It seems good to me also... I get this erroe in the model (in the Plan class) – ParPar Nov 22 '11 at 12:39
  • Is there a stacktrace? What is the message exactly? It may be coming from a different method. – Tudor Nov 22 '11 at 12:40
  • I added this : internal BillingPlan MemberwiseClone() { throw new NotImplementedException(); } and this is exactly where I get the error – ParPar Nov 22 '11 at 12:40
  • You don't have to also add the method MemberwiseClone in your class. It already exists in class Object. – Tudor Nov 22 '11 at 12:42
  • This line I added automatically when I wrote .MemberwiseClone() in the controller. – ParPar Nov 22 '11 at 12:43
  • And if you remove it? By the way, this Clone method needs to be inside your BillingPlan class. – Tudor Nov 22 '11 at 12:44
  • Ok so in the BillingPlan class do you have the method Clone from my answer? – Tudor Nov 22 '11 at 12:56
  • If I replace it like you said - I get another error ("An unhandled exception of type 'System.StackOverflowException' occurred in Sarin.Backoffice.DLL "), and if I remove it - then I have this red wavy line under the .MemberwiseClone() in the controller.. – ParPar Nov 22 '11 at 13:00
  • Yes I do, I have this: internal Plan MemberwiseClone() { throw new NotImplementedException(); } public Plan clone() { return (Plan)this.MemberwiseClone(); } – ParPar Nov 22 '11 at 13:02
  • Ok but you don't need the MemberwiseClone() method, it's inherited from Object. Define only the second one. – Tudor Nov 22 '11 at 13:04
  • I don't understand, I can't remove it on the one hand, and in the other hand it has to return something – ParPar Nov 22 '11 at 13:08
  • Ok what I am trying to say is that all you have to do is to put a method "clone" like public Plan clone() { return (Plan)this.MemberwiseClone(); } in your Plan class. That's it, nothing else. – Tudor Nov 22 '11 at 13:18
  • Well, I did it from the beginning. Thanks. – ParPar Nov 22 '11 at 13:22
  • Please take this discussion to a chatroom. – Lasse V. Karlsen Nov 22 '11 at 13:27
  • Vote my qestion so I'll have 20 reputation, so I'll be able to take it to a chatroom. – ParPar Nov 22 '11 at 13:33
0

give your Plan class a copy constructor and instead of returning View(oldPlan) return View(Plan(oldPlan))

DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
0

For my question I didn't need to use something complicated at all.

All I needed to to is to create two get actions, one with blank fields and another with fields full with the edited object.

the two views send their parameters to the same post action, that gets the sent object and add it to the database. when I write: db.PlanSet.AddObject(plan); -it automatically adds the same object with new Id, without removing or changing the original object.

Good Luck!!

ParPar
  • 7,355
  • 7
  • 43
  • 56