2013-04-06 49 views
1

我想知道我的用戶選擇哪個決定。我的表單有兩個提交按鈕用於兩個決定。它應該是一種還是兩種?

方法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 

有什麼真正的區別?你會去哪,爲什麼?

回答

1

兩個都是有效的,但我會親自去第一個。

我這樣做的原因是標記稍微乾淨一些。表格的目的是供用戶作出決定,這些元素IMO應該組合在一起,例如稱爲frmDecision的表格。

第二個原因是在第二個例子中的代碼是ambigious。

例如,在後面

If Decision Then 

的代碼讀取如果判決等於真實的,那必要不表明AcceptDecline。同時將一個參數傳遞迴名爲Decision的服務器,其值爲true或false,這是一個小小的缺陷。

我發現第一個例子更清潔,雖然你可以使用資源文件,所以你不必使用魔法字符串。

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" /> 
+0

之間的公共代碼,這是一個VB的解決方案而不是C#。其次,我相信你提供的解決方案對於將價值傳遞迴服務器的情況是過分的。 – 2013-04-06 17:20:01

+0

我同意達倫在所有方面。 – dbasnett 2013-04-06 17:26:51

+0

我希望這不是一個問題,翻譯C#到VB,有很多轉換器可用(f.e. http://converter.telerik.com/)。至於矯枉過正 - 我沒有得到,你在哪裏找到它?這與ValueProvider中的對名稱+值與樣本#1中的值相同,但對AJAX調用f.e.更有效得多。你可以調用接受/拒絕行爲,而不是一個複雜的行爲,裏面有很多條件,向它傳遞多餘的選項(比如「決定」)等。我希望你明白可能有兩個以上的選擇和情況#1成爲... – Stan 2013-04-06 17:34:30

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