ASP.NET MVC实现多个按钮提交的方法

所属分类: 网络编程 / ASP.NET 阅读数: 1534
收藏 0 赞 0 分享

有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。

 

如果是用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;
} 


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多