我的服务层将带有警告/成功/信息/错误消息的 DTO 发送回控制器,该控制器继承自我自动处理消息的基本控制器。
我不知道我的实现是否完全是一派胡言,我真的想要一些建议!
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 alert-@item.Key">
@item.Value
</div>
End If
Next