案例#1 不可扩展。When number of choices will grow it will be hard to manage. 并且在操作之间没有清晰的分离,当您通过 AJAX 调用它们时这可能很有用。案例#2 是多余的。我更喜欢第三种方式。起初我有名称选择器属性(我已经从 C# 转换它,所以不确定它是否正常工作):
Imports System
Imports System.Web
Imports System.Web.Mvc
Imports System.Reflection
Namespace MySpace
<AttributeUsage(AttributeTargets.Method, AllowMultiple := False, Inherited := True)> _
Public Class SubmitActionAttribute
Inherits System.Web.Mvc.ActionNameSelectorAttribute
#Region "-- Constants -----------------------------------------------------------------------"
Friend Const ACTION_PREFIX As String = "action:"
Private Const ROUTE_ACTION As String = "action"
#End Region
Private _Name As String = Nothing
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public Overrides Function IsValidName(controllerContext As ControllerContext, actionName As String, methodInfo As MethodInfo) As Boolean
Dim name As String = If(_Name, methodInfo.Name)
Dim key As String = String.Concat(SubmitActionAttribute.ACTION_PREFIX, name)
Dim value As ValueProviderResult = controllerContext.Controller.ValueProvider.GetValue(key)
If value IsNot Nothing Then
controllerContext.RouteData.Values(SubmitActionAttribute.ROUTE_ACTION) = name
Return True
End If
Return False
End Function
Public Sub New(name As String)
_Name = name
End Sub
Public Sub New()
End Sub
End Class
End Namespace
然后我将此属性添加到控制器的操作中
<SubmitAction("Accept")> _
Public Function AcceptSmth() As ActionResult
End Function
并在 HTML 中使用:
<input type="submit" value="Any value for caption" name="action:Accept" />