MVC RedirectAction从创建到编辑

时间:2013-03-12 15:59:24

标签: asp.net-mvc vb.net

         <HttpPost()> _
        Function Create(ByVal cbadata As CBAData) As ActionResult
            If ModelState.IsValid Then
                db.CBADatas.Add(cbadata)
                '    db.SaveChanges()
                Return RedirectToAction("Edit", "CBAData", cbadata)
            End If
        End Function


    '
    ' GET: /CBAData/Edit/5

    Function Edit(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim cbadata As CBAData = db.CBADatas.Find(id)
        If IsNothing(cbadata) Then
            Return HttpNotFound()
        End If
        ViewBag.idCustomer = New SelectList(db.Customers, "id", "Name", cbadata.idCustomer)

        Return View(cbadata)
    End Function

    '
    ' POST: /CBAData/Edit/5

    <HttpPost()> _
    Function Edit(ByVal cbadata As CBAData) As ActionResult
        If ModelState.IsValid Then
            db.Entry(cbadata).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        ViewBag.idCustomer = New SelectList(db.Customers, "id", "Name", cbadata.idCustomer)
        Return View(cbadata)
    End Function

我正在尝试使用“创建”视图作为创建新CBAData项目的启动点。我希望他们选择一些简单的信息,然后继续编辑视图,如果模型有效,最终将保存数据。

上面的代码无法正确导航。当它到达db.CBADatas上的Find方法时,它不存在于列表中。这可能吗?

是否因为包含上下文在内的所有内容都被编辑并重新创建了编辑视图?

1 个答案:

答案 0 :(得分:0)

调用

之类的编辑操作
<HttpPost()> _
    Function Create(ByVal cbadata As CBAData) As ActionResult
        If ModelState.IsValid Then
            db.CBADatas.Add(cbadata)
            '    db.SaveChanges()
            Return RedirectToAction("Edit", "CBAData", new{ id = cbadata})
        End If
    End Function