【问题标题】:How can I implement a copy action that reuses the create and edit views?如何实现重复使用创建和编辑视图的复制操作?
【发布时间】:2014-10-16 20:55:37
【问题描述】:

我想让我的用户使用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在第三位,模型绑定看到了这个。

那么,我应该如何实现 Copy 以便我可以复制对象 123,然后忘记所有关于该 id 的信息,以避免这个问题?如果我应该使用完全不同的设计模式,那也很好。

【问题讨论】:

标签: asp.net-mvc vb.net


【解决方案1】:

将表单操作覆盖为""。这意味着将使用当前 URL。

【讨论】:

    【解决方案2】:

    如果我理解正确的话....只需在 routeValues 属性中将 id 设置为 null 或 0

    在剃须刀中使用 c#

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

    在控制器中:

    public ActionResult Copy(int? Id)
    // action code
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多