1

我想知道我的用户选择了哪个决定。

方法一

方法一

HTML

<form method="post">
    <input type="submit" value="Accept" name="decision" />
    <input type="submit" value="Decline" name="decision" />
</form>

VB.NET

If Decision = "Accept" Then
    ' Do this
ElseIf Decision = "Decline" Then
    ' Do that
End If

方法二

方法二

HTML

<form method="post">
    <input type="hidden" name="decision" value="true" />
    <input type="submit" value="Accept" />
</form>

<form method="post">
    <input type="hidden" name="decision" value="false" />
    <input type="submit" value="Decline" />
</form>

VB.NET

If Decision Then
    ' Do this
Else
    ' Do that
End If

有什么真正的区别吗?你会选择哪个,为什么?

4

3 回答 3

1

两者都是有效的,但我个人会选择第一个。

我这样做的原因是标记更清晰。表单的目的是让用户做出决定,这些元素 IMO 应该组合在一起,例如名为frmDecision.

第二个原因是第二个示例中的代码不明确。

例如,在后面的代码中

 If Decision Then

如果Decision 等于true,则读取该值不表示AcceptDecline。同样将参数传递回名为 Decision 的服务器,其值为 true 或 false ,这有点麻烦。

我发现第一个示例更清洁,尽管您可以使用资源文件,因此您不必使用魔术字符串。

于 2013-04-06T17:08:54.283 回答
0

案例#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" />
于 2013-04-06T17:17:33.937 回答
0

我会选择第一个。您提供的 vb 代码应该看起来更像这样

Private Sub Accept_Click(sender As Object, e As EventArgs) Handles Accept.Click
    'do this
End Sub

Private Sub Decline_Click(sender As Object, e As EventArgs) Handles Decline.Click
    'do that
End Sub

或者如果两者之间有共同的代码

Private Sub Accept_Click(sender As Object, e As EventArgs) Handles Accept.Click
    doThisOrThat(True)
End Sub

Private Sub Decline_Click(sender As Object, e As EventArgs) Handles Decline.Click
    doThisOrThat(False)
End Sub

Private Sub doThisOrThat(decision As Boolean)
    If decision Then
        'do this
    Else
        'do that
    End If
End Sub
于 2013-04-06T17:25:43.990 回答