2012-05-22 107 views
0

我有兩種提交方式在我的頁面上有一個表單。第一個是錨定標記,另一個是提交按鈕,它們都有不同的行爲。ASP.Net MVC 3多次提交

如何分配給每個單獨的操作方法?謝謝。

+1

您可以在提交之前使用javascript更改操作方法。 –

回答

2

它確實取決於錨標記的作用 - 大概是它觸發在JavaScript中提交?

如果它實際上只是在執行GET而不是,那麼您可以按照@dbaseman的建議 - 爲兩種請求類型分別設置操作方法。

但是,如果錨沒有JavaScript的提交,然後我的偏好是簡單地給提交按鈕的名稱,以便您可以檢測到它的一個操作方法在服務器上,然後用叉子叉從那裏代碼:

<submit name="fromButtom" value="Submit" /> 

然後你的操作方法:

public ActionResult Foo(string fromButton) 
{ 
    //if 'fromButton' contains 'Submit' then you assume it was the button. 
} 

更妙的是你可以使用一個<button>代替,然後你就可以從價值離婚顯示的文本,該按鈕提交(如果你是本地化的頁面是有用的) :

<button name="submitMethod" value="fromButton">Submit</button> 

現在,您可以在您的操作方法上有一個submitMethod參數,其中您尋找'fromButton'

無論哪種方式 - 錨定標記/ JavaScript(如果這是你如何做)不會提交此值,因爲按鈕的值只在點擊時提交。

+0

好,我可以使用這個解決方案,但是我使用提交按鈕中的值作爲我的顯示文本,我不想在我的代碼中依賴它。我使用的是一個自定義的HTML幫手,如下所示 – ancdev

1

只需使用MVC HttpPostHTTPGET在不同版本的動作的屬性:

[HttpPost] 
public ActionResult FormAction(Model model, string method = post) { ... } 

[HttpGet] 
public ActionResult FormAction(Model model) { ... } 
+0

您可能想要在這裏更改方法名稱 - 不會編譯。你需要不同的方法名稱和一個'[ActionName]':) –

+0

@AndrasZoltan除非我失去它(完全可能,因爲它是凌晨2點),應該complie。你看到什麼問題? – McGarnagle

+0

哈哈 - 我去過那兒了!兩種方法具有相同的名稱和相同的參數;即他們是重複:) –

0

如果這些方法完全不同,那麼您應該使用JavaScript來使它們的行爲不同。使用jQuery例如,它看起來像:

$("#sbutton").click(function() { 
    alert("Submit button clicked!"); 
    // Do whatever you need - validate/initialize-fields/etc. 
    return false; // This is required to prevent normal form submit 
}); 

$("#slink").click(function() { 
    alert("Link clicked!"); 
    // Do whatever you need - validate/initialize-fields/etc. 
    return false; // This is required to prevent normal form submit 
}); 
0

我想補充另一種解決辦法,如果你想創造的東西,將與JavaScript的工作,關閉,這不是方便更改按鈕的名稱和值,那麼你可以試試這是我在生產代碼中使用的。

創建這樣一個幫手:

using System; 
using System.Reflection; 
using System.Web.Mvc; 

namespace Whatever 
{ 
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
    public class MultipleSubmitAttribute : ActionNameSelectorAttribute 
    { 
     public string Name { get; set; } 
     public string Argument { get; set; } 

     public override bool IsValidName(ControllerContext controllerContext, 
     string actionName, MethodInfo methodInfo) 
     { 
      bool isValidName = false; 
      string keyValue = string.Format("{0}:{1}", Name, Argument); 
      var value = controllerContext.Controller.ValueProvider.GetValue(keyValue); 
      if (value != null) 
      { 
       value = new ValueProviderResult(Argument, Argument, null); 
       controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument; 
       isValidName = true; 
      } 

      return isValidName; 
     } 
    } 
} 

現在在你的表格,你可以做這樣的事情:

<input type="submit" name="action:DoSomething" value="Do something" /> 
<input type="submit" name="action:DoSomethingElse" value="Do something else" /> 

然後在你的控制器,你可以裝飾你這樣的操作方法:

[HttpPost] 
[MultipleSubmit(Name = "action", Argument = "DoSomething")] 
public ActionResult DoSomething(ViewModel vm) 
{ ... } 

[HttpPost] 
[MultipleSubmit(Name = "action", Argument = "DoSomethingElse")] 
public ActionResult DoSomethingElse(ViewModel vm) 
{ ... } 
+0

如果我有兩個按鈕,而不是標籤和一個按鈕 – ancdev

+0

這個解決方案本來可以工作的很好,我認爲它可能會派上用場,以備將來使用。另外,你也可以使用javascript這樣的''標籤來提交它:'onclick =「$(this).parents('form')。submit(); return false;'' –