我的表单有两个提交按钮,用于两个决定。应该是一种还是两种形式?

时间:2013-04-06 17:03:43

标签: asp.net-mvc vb.net forms coding-style

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

方法1

Method 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

方法2

Method 2

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

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

3 个答案:

答案 0 :(得分:1)

两者都有效,但我会亲自去第一个。

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

第二个原因是第二个例子中的代码是暧昧的。

例如,在

背后的代码中
 If Decision Then

如果决定等于真,则读取,必要时不表示AcceptDecline。还将参数传递回名为Decision的服务器,其值为true或false,这有点不对劲。

我发现第一个示例清理器,虽然您可以使用资源文件,因此您不必使用魔术字符串。

答案 1 :(得分:0)

案例#1不可扩展。当选择的数量增加时,将很难管理。并且不能清除动作之间的分离,当您通过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" />

答案 2 :(得分: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