我如何复制对象? [英] How do I duplicate an object?

查看:166
本文介绍了我如何复制对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划列表,或者不管是什么,我不希望任何人将被删除。
这就是为什么当有人选择编辑选项 - 其实我是想添加一个新的计划,以相同的参考,而只是一个新的ID。然后,我不在乎在所有什么样的变化将在其作出的编辑视图(这实际上是在创建视图)。

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.

下面是我的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.

我怎么能复制它,这样会在视图中所做的任何改变 - 将与另一个计划ID被保存?
希望我自己清楚,我很高兴得到一些帮助!

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 !

推荐答案

有关我的问题我并不需要用的东西都复杂化了。

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.

两个视图发送其参数为相同的交动作,即获取发送对象,并把它添加到数据库中。当我写: db.PlanSet.AddObject(计划); - 它会自动添加与新的ID相同的对象,而不删除或更改原始对象

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 !!

Good Luck!!

这篇关于我如何复制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆