国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET MVC實現多個按鈕提交的方法

2024-07-10 12:54:21
字體:
來源:轉載
供稿:網友

有時候我們的網頁設置需要多個按鈕提交不同的數據,這在開發過程中是很常使用的,今天就讓錯新技術頻道小編帶大家一起了解ASP.NET MVC實現多個按鈕提交的方法吧!

有時候會遇到這種情況:在一個表單上需要多個按鈕來完成不同的功能,比如一個簡單的審批功能。

?

如果是用webform那不需要討論,但asp.net mvc中一個表單只能提交到一個Action處理,相對比較麻煩點。?

方法一:使用客戶端腳本?

比如我們在View中這樣寫:

<inputtype="submit"value="審核通過"onclick='this.form.action="<%=Url.Action("Action1")%>/><inputtype="submit"value="審核不通過"onclick='this.form.action="<%=Url.Action("Action2")%> /><inputtype="submit"value="返回"onclick='this.form.action="<%=Url.Action("Action3")%>" />

在點擊提交按鈕時,先改變Form的action屬性,使表單提交到按鈕相應的action處理。?

但有的時候,可能Action1和2的邏輯非常類似,也許只是將某個字段的值置為1或者0,那么分開到二個action中又顯得有點多余了。?

方法二:在Action中判斷通過哪個按鈕提交?

在View中,我們不用任何客戶端腳本處理,給每個提交按鈕加好name屬性:?

<input type="submit" value="審核通過" name="action" /><input type="submit" value="審核不通過" name="action"/><input type="submit" value="返回" name="action"/>

然后在控制器中判斷:

[HttpPost] public ActionResult Index(string action /* 其它參數*/) {  if (action=="審核通過")  {   //  }  else if (action=="審核不通過")  {//  }  else  {   //  } }

幾年前寫asp代碼的時候經常用這樣的方法…?

View變得簡單的,Controller復雜了。

?太依賴說View,會存在一些問題。假若哪天客戶說按鈕上的文字改為“通過審核”,或者是做個多語言版的,那就麻煩了。?

?方法三:使用ActionSelector?

關于ActionSelector的基本原理可以先看下這個POST使用ActionSelector控制Action的選擇。?

使用此方法,我們可以將控制器寫成這樣:

[HttpPost][MultiButton("action1")]public ActionResult Action1(){ // return View();}[HttpPost][MultiButton("action2")]public ActionResult Action2(){ // return View();}

在 View中:?

<input type="submit" value="審核通過" name="action1" /><input type="submit" value="審核不通過" name="action2"/><input type="submit" value="返回" name="action3"/>

此時,Controller已經無須依賴于按鈕的Value值。?

MultiButtonAttribute的定義如下:

public class MultiButtonAttribute : ActionNameSelectorAttribute{ public string Name { get; set; } public MultiButtonAttribute(string name) {  this.Name = name; } public override bool IsValidName(ControllerContext controllerContext,  string actionName, System.Reflection.MethodInfo methodInfo) {  if (string.IsNullOrEmpty(this.Name))  {   return false;  }  return controllerContext.HttpContext.Request.Form.AllKeys.Contains(this.Name); }}

方法四:改進

Controller:?

[HttpPost] [MultiButton(Name = "delete", Argument = "id")] public ActionResult Delete(string id) { var response = System.Web.HttpContext.Current.Response; response.Write("Delete action was invoked with " + id); return View(); } 

View:

<input type="submit" value="not important" name="delete" /><input type="submit" value="not important" name="delete:id" />

MultiButtonAttribute定義:?

代碼

?

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultiButtonAttribute : ActionNameSelectorAttribute { public string Name { get; set; } public string Argument { get; set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { var key = ButtonKeyFrom(controllerContext); var keyIsValid = IsValid(key); if (keyIsValid) { UpdateValueProviderIn(controllerContext, ValueFrom(key)); } return keyIsValid; } private string ButtonKeyFrom(ControllerContext controllerContext) { var keys = controllerContext.HttpContext.Request.Params.AllKeys; return keys.FirstOrDefault(KeyStartsWithButtonName); } private static bool IsValid(string key) { return key != null; } private static string ValueFrom(string key) { var parts = key.Split(":".ToCharArray()); return parts.Length < 2 ? null : parts[1]; } private void UpdateValueProviderIn(ControllerContext controllerContext, string value) { if (string.IsNullOrEmpty(Argument)) return; controllerContext.Controller.ValueProvider[Argument] = new ValueProviderResult (value, value, null); } private bool KeyStartsWithButtonName(string key) { return key.StartsWith(Name, StringComparison.InvariantCultureIgnoreCase); } } //如果是在MVC 2.0中的話,將UpdateValueProviderIn方法改為:private void UpdateValueProviderIn(ControllerContext controllerContext, string value){ if (string.IsNullOrEmpty(Argument)) return; controllerContext.RouteData.Values[this.Argument] = value;} 上文錯新技術頻道小編詳細介紹了ASP.NET MVC實現多個按鈕提交的方法,看完后你感覺對你有幫助嗎?為了讓更多朋友輕松獲得全面的專業知識,我們梳理了更多內容,一起來看看吧!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 报价| 亳州市| 大宁县| 波密县| 聊城市| 五原县| 崇信县| 西畴县| 鸡泽县| 台湾省| 曲周县| 开鲁县| 东辽县| 黑河市| 威海市| 本溪市| 郸城县| 中西区| 泸定县| 濉溪县| 文安县| 寿宁县| 兴城市| 远安县| 比如县| 望都县| 蕲春县| 诸城市| 阿克陶县| 蓬莱市| 琼海市| 治县。| 西乡县| 平顶山市| 馆陶县| 秭归县| 新丰县| 巴中市| 伊宁县| 万全县| 皋兰县|