创建对象后,虽然正确创建了对象,但我的页面不会继续显示详细信息页面。
以下是我的代码:
Function Create(ByVal collection As FormCollection) As ActionResult
Dim _NieuweKlant As New Domain.Slave.Klant
Try
If ModelState.IsValid Then
TryUpdateModel(_NieuweKlant, collection)
_NieuweKlant.UpdatedON = Now
_NieuweKlant.LaatsteWijzigingGebruiker = Now
'_NieuweKlant.LaatsteActie = Now
KlantService.createKlant(_NieuweKlant)
KlantService.SaveKlant()
'check validstate
Return Details(_NieuweKlant.KlantID)
End If
Catch ex As System.Data.Entity.Validation.DbEntityValidationException
Dim Errors = ex.EntityValidationErrors.First
For Each propertyError In Errors.ValidationErrors
ModelState.AddModelError(propertyError.PropertyName, propertyError.ErrorMessage)
Next
Return View(_NieuweKlant)
Catch ex As System.Data.Entity.Infrastructure.DbUpdateException
Return View(_NieuweKlant)
Catch Ex As Exception
' Console.Out.Write("Bericht:" & vbCrLf & Ex.Message)
' Console.Out.Write("InnerException: " & vbCrLf & Ex.InnerException.ToString)
Return View()
End Try
End Function
我也尝试过RedirectToAction,调试时似乎没有错误。它只是不会重定向或转到其他操作。
答案 0 :(得分:1)
如果您想重定向,则需要使用RedirectToAction
,如下所示:
Return RedirectToAction("Details", new { id = _NieuweKlant.KlantID })
作为第二个参数,我们指定了id动作参数,以便在页面重定向时填充此参数:
Function Details(ByVal Id As Integer) As ActionResult
当然,只有在没有抛出异常的情况下才会发生这种重定向。
如果您使用AJAX调用Create
操作,则说明重定向方面可能会有所不同。