I want to be enable my user to click on an existing entity with, say, Id=123
, fetch and copy that object, and then let the user edit it and create a new object using the same view that I'm using to create or edit. I tried the following action method:
' GET: /controller/copy/id
Function Copy(ByVal id As Integer) As ActionResult
Dim obj = db.MyTable.Find(id)
If obj.IsNothing() Then
Return HttpNotFound()
End If
'blank out the id to flag that this is a new entity
obj.Id = 0
'reuse the editing view
Return View("Edit", obj)
End Function
This renders fine, but the form HTML looks like this:
<form action="/Controller/Copy/123" method="post">
And when I post back, the passed model object has Id=123, same as the object being copied. I think this must be because the route /Controller/Copy/123
has the id in the third position, and model binding sees this.
So, how should I implement Copy so that I can copy object 123 but then forget all about that id, to avoid this problem? If there's an entirely different design pattern I should be using, that's fine too.