代码之家  ›  专栏  ›  技术社区  ›  Endy Tjahjono

如何使用后重定向get处理ASP.NET MVC中的并发冲突?

  •  0
  • Endy Tjahjono  · 技术社区  · 15 年前

    我使用后重定向获取(prg)模式将实体保存在ASP.NET MVC 2中。控制器方法有“保存”(插入或更新数据库)和“编辑”(检索用户输入)。在“保存”中,我在保存之前通过检查实体的“版本”列进行修改检查。如果其他人修改了实体,“版本”列将不匹配,我将向用户发送错误消息。

    为了维护错误消息,我使用了modelstate.merge的“edit”方法。这个机制的问题在于,用户输入被维护,而用户看不到其他用户所做的修改。我通过在添加并发冲突消息之前清除ModelState来避免这个问题。

    但我觉得这个解决方案并不理想。如何处理ASP.NET MVC中的并发冲突?

    以下是编辑方法:

        Public Function Edit() As ActionResult
            Dim theevent As AEvents
    
            If TempData("ModelState") IsNot Nothing And Not ModelState.Equals(TempData("ModelState")) Then
                ModelState.Merge(CType(TempData("ModelState"), ModelStateDictionary))
            End If
    
            If RouteData.Values.ContainsKey("id") Then
                theevent = NHibGet.EventWithPricingsByCode(RouteData.Values("id"))
            Else
                theevent = New AEvents
            End If
    
            Dim InputTemplate As New EventEdit With {.EventDate = theevent.EventDate, .EventName = theevent.EventName, .IsActive = theevent.IsActive}
            If theevent.Template IsNot Nothing Then
                InputTemplate.TemplateID = theevent.Template.ID
            End If
    
            Dim templates As IList(Of SeatTemplates) = NHibGet.TemplatesActive
            ViewData("templates") = templates
    
            ViewData("eventcode") = theevent.Code
            ViewData("editversion") = theevent.Version
    
            Return View(InputTemplate)
        End Function
    

    “保存”的代码如下:

        Public Function Save(ByVal id As Integer, ByVal UserData As EventEdit, ByVal EditVersion As Integer) As ActionResult
            Dim theevent As AEvents
            If id = 0 Then
                theevent = New AEvents
            Else
                theevent = NHibGet.EventByCode(id)
            End If
    
            If theevent.Version <> EditVersion Then
                ModelState.Clear()
                ModelState.AddModelError("", "The event is modified by someone else")
                Return RedirectToAction("Edit", New With {.id = id})
            End If
    
            If Not ModelState.IsValid Then
                TempData("ModelState") = ModelState
                Return RedirectToAction("Edit", New With {.id = id})
            End If
    
            theevent.EventDate = UserData.EventDate
            theevent.EventName = UserData.EventName
            theevent.IsActive = UserData.IsActive
            theevent.Template = MvcApplication.CurrentSession.Load(Of SeatTemplates)(UserData.TemplateID)
    
            Using trans As NHibernate.ITransaction = MvcApplication.CurrentSession.BeginTransaction
                MvcApplication.CurrentSession.SaveOrUpdate(theevent)
    
                Try
                    trans.Commit()
                Catch ex As NHibernate.ADOException
                    trans.Rollback()
                    Throw ex
                End Try
            End Using
    
            Return RedirectToAction("Edit", New With {.id = theevent.Code})
        End Function
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   dariol    14 年前

    在这种情况下,我总是指示用户刷新页面。它还让他有机会查看输入的字段值。