是否有一些簡單的方法能夠處理來自同一表單的多個提交按鈕? 例: app
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %> <input type="submit" value="Send" /> <input type="submit" value="Cancel" /> <% Html.EndForm(); %>
任何想法如何在ASP.NET Framework Beta中執行此操做? 我搜索過的全部示例中都包含一個按鈕。 ide
大衛·芬德利(David Findley)在他的ASP.Net Weblog上撰寫了有關執行此操做的三種不一樣選擇的信息。 spa
閱讀本文中具備相同形式的多個按鈕,以查看他的解決方案以及每種解決方案的優缺點。 恕我直言,他提供了一種很是優雅的解決方案,該方案利用了裝飾動做的屬性。 code
對於每一個提交按鈕,只需添加: orm
$('#btnSelector').click(function () { $('form').attr('action', "/Your/Action/); $('form').submit(); });
我試圖對全部解決方案進行綜合,並建立了一個[ButtenHandler]屬性,該屬性使處理表單上的多個按鈕變得容易。 blog
我已經在ASP.NET MVC中的 CodeProject 多個參數化(可本地化)表單按鈕上對其進行了描述。 索引
要處理此按鈕的簡單狀況: ip
<button type="submit" name="AddDepartment">Add Department</button>
您將擁有相似如下操做方法的內容: element
[ButtonHandler()] public ActionResult AddDepartment(Company model) { model.Departments.Add(new Department()); return View(model); }
請注意,按鈕的名稱如何與操做方法的名稱匹配。 本文還介紹瞭如何具備帶有值的按鈕和帶有索引的按鈕。 get
我沒有足夠的表明在正確的位置發表評論,但我成天都在此發表評論,因此想分享。
嘗試實現「 MultipleButtonAttribute」解決方案時, ValueProvider.GetValue(keyValue)
將錯誤地返回null
。
原來,當我應該將System.Web.MVC版本3.0設置爲4.0時(其餘程序集是4.0)。 我不知道爲何個人項目沒法正確升級,而且我沒有其餘明顯的問題。
所以,若是您的ActionNameSelectorAttribute
不起做用,請檢查。
//model public class input_element { public string Btn { get; set; } } //views--submit btn can be input type also... @using (Html.BeginForm()) { <button type="submit" name="btn" value="verify"> Verify data</button> <button type="submit" name="btn" value="save"> Save data</button> <button type="submit" name="btn" value="redirect"> Redirect</button> } //controller public ActionResult About() { ViewBag.Message = "Your app description page."; return View(); } [HttpPost] public ActionResult About(input_element model) { if (model.Btn == "verify") { // the Verify button was clicked } else if (model.Btn == "save") { // the Save button was clicked } else if (model.Btn == "redirect") { // the Redirect button was clicked } return View(); }