我的服務層向控制器發回一個帶有警告/成功/信息/錯誤消息的DTO,該控制器從基本控制器繼承,在此處我以消息方式處理消息。MVC - 自動處理用戶通知
我不知道我的執行是否是完全的廢話,我真的想要一些建議!
DTO
Public Class ExecutionResult
Public Enum AlertType
Warning
Success
Info
[Error]
End Enum
Public Property Type() As AlertType
Public Property Message() As String
End Class
基地控制器: (與我可以從每一個控制器訪問屬性executionResult)
Public Class BaseController
Inherits System.Web.Mvc.Controller
Protected Overloads Overrides Sub OnActionExecuted(ByVal ctx As ActionExecutedContext)
Alert()
End Sub
Public Property executionResult As New ExecutionResult
Public Sub Alert()
If Not String.IsNullOrWhiteSpace(Me.executionResult.Message) Then
TempData.Add(Me.executionResult.Type, Me.executionResult.Message)
End If
End Sub
End Class
控制器:
...
Inherits BaseController
...
Function SomeFunction() As ActionResult
executionResult = _service.SomeFunctionInTheServiceLayer(viewModel)
End Function
然後我從主頁
@For Each item As KeyValuePair(Of String, Object) In TempData
If (TempData.ContainsKey(item.Key)) Then
@<div class="alert [email protected]">
@item.Value
</div>
End If
Next
如果它適用於你和你對解決方案感到滿意,沒關係,不是嗎? – Kenneth 2013-05-13 08:58:25
只想知道這種情況是否存在「最佳實踐」,因爲我認爲每個人都必須在MVC網絡應用程序中以某種方式通知用戶。 – stare 2013-05-13 09:02:39