亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET MVC實(shí)現(xiàn)多個(gè)按鈕提交的方法

 更新時(shí)間:2016年09月18日 15:56:07   作者:無常  
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)多個(gè)按鈕提交的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

有時(shí)候會(huì)遇到這種情況:在一個(gè)表單上需要多個(gè)按鈕來完成不同的功能,比如一個(gè)簡(jiǎn)單的審批功能。

 

如果是用webform那不需要討論,但asp.net mvc中一個(gè)表單只能提交到一個(gè)Action處理,相對(duì)比較麻煩點(diǎn)。 

方法一:使用客戶端腳本 

比如我們?cè)赩iew中這樣寫:

<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")%>" />

在點(diǎn)擊提交按鈕時(shí),先改變Form的action屬性,使表單提交到按鈕相應(yīng)的action處理。 

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

方法二:在Action中判斷通過哪個(gè)按鈕提交 

在View中,我們不用任何客戶端腳本處理,給每個(gè)提交按鈕加好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 /* 其它參數(shù)*/)
 {
  if (action=="審核通過")
  {
   //
  }
  else if (action=="審核不通過")
  {
//
  }
  else
  {
   //
  }
 }

幾年前寫asp代碼的時(shí)候經(jīng)常用這樣的方法… 

View變得簡(jiǎn)單的,Controller復(fù)雜了。

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

 方法三:使用ActionSelector 

關(guān)于ActionSelector的基本原理可以先看下這個(gè)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"/>

此時(shí),Controller已經(jīng)無須依賴于按鈕的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);
 }
}

方法四:改進(jìn)

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;
} 


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論