2009-11-11 205 views
108

我有我的MVC格式上的兩個按鈕:MVC其提交按鈕被按下

<input name="submit" type="submit" id="submit" value="Save" /> 
<input name="process" type="submit" id="process" value="Process" /> 

從我的控制器動作我怎麼知道已經按下哪一個?

+0

爲什麼不只是添加的onclick事件到這些按鈕,走自己的AJAX調用,這將去他們適當的方法?即:''? – ragerory 2015-03-26 18:25:28

回答

141

名稱既您的提交按鈕提交的相同

<input name="submit" type="submit" id="submit" value="Save" /> 
<input name="submit" type="submit" id="process" value="Process" /> 

然後在你的控制器中獲得的價值。只有點擊的按鈕纔會傳遞其值。

public ActionResult Index(string submit) 
{ 
    Response.Write(submit); 
    return View(); 
} 

您當然可以評估該值,以便用開關塊執行不同的操作。

public ActionResult Index(string submit) 
{ 
    switch (submit) 
    { 
     case "Save": 
      // Do something 
      break; 
     case "Process": 
      // Do something 
      break; 
     default: 
      throw new Exception(); 
      break; 
    } 

    return View(); 
} 
+12

請注意本地化可能會給此解決方案帶來的問題,達林的解決方案對此無法解決。 – 2009-11-27 08:23:33

+0

具有相同名稱的兩個輸入會導致數組以每個值的一個元素髮布,而不是隱含的單個值。所以我希望我可以說,否則,因爲我希望/試圖使用這個,但這是行不通的。 – 2017-02-09 20:20:21

43
<input name="submit" type="submit" id="submit" value="Save" /> 
<input name="process" type="submit" id="process" value="Process" /> 

而在你的控制器動作:

public ActionResult SomeAction(string submit) 
{ 
    if (!string.IsNullOrEmpty(submit)) 
    { 
     // Save was pressed 
    } 
    else 
    { 
     // Process was pressed 
    } 
} 
+1

我想通過簡單地將它添加到參數列表並正確命名它可以添加更多的按鈕。好的解決方案 – GONeale 2011-08-30 00:34:46

0

你不能找出使用Request.Form集合?如果過程是點擊的Request.Form [「過程」]不會是空的

26

這是一個更好的答案,所以我們可以有一個按鈕,文本和值:

http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

</p> 
<button name="button" value="register">Register</button> 
<button name="button" value="cancel">Cancel</button> 
</p> 

和控制器:

public ActionResult Register(string button, string userName, string email, string password, string confirmPassword) 
{ 
if (button == "cancel") 
    return RedirectToAction("Index", "Home"); 
... 

在短它是一個SUBMIT按鈕,但是你使用name屬性來選擇名稱,因爲它更強大您沒有義務在控制器方法參數中的名稱提交或按鈕,您可以隨心所欲地調用它...

+0

謝謝先生,這正是我所需要的 – oHoodie 2015-03-05 08:52:08

5

下面是一個非常簡單的方法,使用自定義的MultiButtonAttribute可以很容易地遵循指令:

http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

總之,讓你的提交按鈕這樣的:

<input type="submit" value="Cancel" name="action" /> 
<input type="submit" value="Create" name="action" /> 

你的行爲是這樣的:

[HttpPost] 
[MultiButton(MatchFormKey="action", MatchFormValue="Cancel")] 
public ActionResult Cancel() 
{ 
    return Content("Cancel clicked"); 
} 

[HttpPost] 
[MultiButton(MatchFormKey = "action", MatchFormValue = "Create")] 
public ActionResult Create(Person person) 
{ 
    return Content("Create clicked"); 
} 

而創建這個類:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
public class MultiButtonAttribute : ActionNameSelectorAttribute 
{ 
    public string MatchFormKey { get; set; } 
    public string MatchFormValue { get; set; } 

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) 
    { 
     return controllerContext.HttpContext.Request[MatchFormKey] != null && 
      controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue; 
    } 
} 
+1

總是最好總結引用的鏈接(博客消失的那一天)。總之,博客倡導者擁有'MultiButtonAttribute'自定義屬性以允許區分提交按鈕。其實是一個很好的主意。 – 2014-08-28 12:51:04

+1

如果'Arnis L.'遵循相同的建議,您可能已經注意到他提供了4年前的完全相同的鏈接:> – 2014-08-28 12:52:23

+0

非常好的點@TrueBlueAussie,我已經添加了代碼摘要 – Owen 2014-09-26 10:40:30

3

這個職位不會回答Coppermill,因爲他已經回答了前很長一段時間。我的文章將有助於誰會尋求這樣的解決方案。首先,我必須說「WDuffy的解決方案是完全正確的」,並且它工作正常,但我的解決方案(不是我的解決方案)將用於其他元素,它使表示層更獨立於控制器(因爲您的控制器依賴於用於顯示按鈕標籤的「值」,該功能對其他語言很重要)。
這裏是我的解決方案,給他們不同的名稱,如:
</p> <input type="submit" name="buttonSave" value="Save"/> <input type="submit" name="buttonProcess" value="Process"/> <input type="submit" name="buttonCancel" value="Process"/> </p>
而你必須在行動像下面指定按鈕的名稱作爲參數:

public ActionResult Register(string buttonSave, string buttonProcess, string buttonCancel){if (buttonSave!= null) 
{//save is pressed 
} 
if (buttonProcess!= null) 
{//Process is pressed 
} 
if (buttonCancel!= null) 
{//Cancel is pressed 
} 


當用戶提交頁面使用其中一個按鈕,只有一個參數會有價值。我想這會對其他人有所幫助。

2

爲了方便,我會說你可以改變你的按鈕如下:

<input name="btnSubmit" type="submit" value="Save" /> 
<input name="btnProcess" type="submit" value="Process" /> 

你的控制器:

public ActionResult Create(string btnSubmit, string btnProcess) 
{ 
    if(btnSubmit != null) 
     // do something for the Button btnSubmit 
    else 
     // do something for the Button btnProcess 
} 
2
// Buttons 
<input name="submit" type="submit" id="submit" value="Save" /> 
<input name="process" type="submit" id="process" value="Process" /> 

// Controller 
[HttpPost] 
public ActionResult index(FormCollection collection) 
{ 
    string submitType = "unknown"; 

    if(collection["submit"] != null) 
    { 
     submitType = "submit"; 
    } 
    else if (collection["process"] != null) 
    { 
     submitType = "process"; 
    } 

} // End of the index method 
+0

這是一個優秀的解決方案發布到其他所有內容,因爲它允許非常複雜的表單數據一致地傳遞,而不需要在每個動作中定製參數集,從而允許控制器邏輯針對複雜表單進行高度自定義。榮譽Fredrik :)我認爲這個FormCollection會通過多種形式? – Stokely 2017-08-24 22:38:01

+0

謝謝Stokely。您可以從多種形式調用此方法,如果您執行ajax調用,則可以在同一個FormCollection中包含多個表單。 – 2017-08-30 05:06:11

15

您可以識別您的按鈕從名稱標記如下, 您需要檢查像你這樣的控制器

if (Request.Form["submit"] != null) 
{ 
//Write your code here 
} 
else if (Request.Form["process"] != null) 
{ 
//Write your code here 
} 
+0

這個技巧對我來說真的很有用,謝謝 – 2016-11-18 07:10:23

+0

當你不想將按鈕的名字傳遞給postactionresult時,這非常有用。 – yogihosting 2017-04-03 12:42:42

+0

在這種情況下,在單元測試中模擬Request類可能會更復雜。 – Muflix 2017-05-29 14:47:36

1

給這兩個按鈕的名稱,並從窗體中檢查值。

<div> 
    <input name="submitButton" type="submit" value="Register" /> 
</div> 

<div> 
    <input name="cancelButton" type="submit" value="Cancel" /> 
</div> 

在控制器端:

public ActionResult Save(FormCollection form) 
{ 
if (this.httpContext.Request.Form["cancelButton"] !=null) 
{ 
    // return to the action; 
} 

if (this.httpContext.Request.Form["submitButton"] !=null) 
{ 
    // save the oprtation and retrun to the action; 
} 
}