2014-10-16 66 views
1

我想讓我的用戶點擊一個現有的實體,例如Id=123,獲取並複製該對象,然後讓用戶編輯它並使用我用來創建的相同視圖創建一個新對象或編輯。我嘗試以下操作方法:如何實現重複使用創建和編輯視圖的複製操作?

' 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 

這使得罰款,但形式HTML看起來像這樣:

<form action="/Controller/Copy/123" method="post"> 

當我回來後,所傳遞的模型對象的ID是= 123,同被複製的對象。我認爲這一定是因爲路線/Controller/Copy/123在第三位有id,模型綁定看到了這個。

那麼,我應該如何實現複製,以便我可以複製對象123,但然後忘記所有關於該ID,以避免這個問題?如果我應該使用完全不同的設計模式,那也沒關係。

+0

聲音就像你需要在單擊副本時重新回到服務器並重新創建創建表單一樣,但是此時ID不在那裏,但其餘值全部輸入。 – Gjohn 2014-10-16 21:03:26

+0

可能du plexate [HiddenFor(x => x.Id)正在由UrlParameter而不是ViewModel填充](http://stackoverflow.com/questions/8748940/hiddenforx-x-id-is-being-populated-by-所述-urlparameter-代替的最-viewm) – RubberDuck 2015-09-01 20:22:39

回答

0

重寫表單動作爲""。這意味着將使用當前的URL。

0

如果我理解你的權利....只是設置ID在形式routeValues財產

在剃鬚刀使用C#

@using (Html.BeginForm("copy", "controller", FormMethod.Post, new { id = null /* or = 0 */ })) 
// form code 

在控制器空或0:

public ActionResult Copy(int? Id) 
// action code 
相關問題