All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET,2.0,多文件上传小经验_[Asp.Net教程]
Silverlight,2,(beta1)数据操作(6)——使用LINQ,to,SQL进行数据_[Asp.Net教程]
实现GridView行背景色交替、点击行变色(javascript-css)_[Asp.Net教程]
Asp.Net,Ajax,学习笔记12,基于AJAX,Library扩展客户端组件_[Asp.Net教程]
C#教程:在GDI+中缩放图像
C#教程:读取注册表信息
C#中如何读写INI文件_.net资料_编程技术
在ASP.NET里得到网站的域名_[Asp.Net教程]
无废话C#设计模式之三:Abstract,Factory_.net资料_编程技术
Web服务器IIS6的PHP最佳配置方法_[PHP教程]
Reading number is top 10 pictures
NeedWallpaper12
The money of more than 100 countries and regions10
Sora aoi on twitter3
Small QiShu -- ShuangShuangPan2
Sora aoi be huged like a bear
女人化妆前后的对比
Angie Chiu vijara myth2
In the world the most mysterious 21 place landscape2
清纯性感的美眉2
The real super beauty5
Download software ranking
双旗镇刀客A
在线棋牌游戏3.05版
WebService在.NET中的实战应用教学视频 → 第5集
变速齿轮3.26
小黑猫大战两米大花蛇
Love the forty days
Red cliff
Professional killers2 for Android
传奇私服架设教程-chm
Visual C++界面编程技术
归海一刀 published in(发表于) 2014/1/30 1:24:22 Edit(编辑)
ASP.NET表单实现多个按钮完成多个功能_[Asp.Net教程]

ASP.NET表单实现多个按钮完成多个功能_[Asp.Net教程]

ASP.NET表单实现多个按钮完成多个功能_[Asp.Net教程]
在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能
image
如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。

方法一:使用客户端脚本

比如我们在View中这样写:

以下为引用的内容:

";’ />
";’ />
";’ />

在点击提交按钮时,先改变Form的action属性,使表单提交到按钮相应的action处理。

但有的时候,可能Action1和2的逻辑非常类似,也许只是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。

方法二:在Action中判断通过哪个按钮提交

在View中,我们不用任何客户端脚本处理,给每个提交按钮加好name属性:

以下为引用的内容:





然后在控制器中判断:

以下为引用的内容:

[HttpPost]
public ActionResult Index(string action /* 其它参数*/)
{
if (action=="审核通过")
{
//
}
else if (action=="审核不通过")
{
//
}
else
{
//
}
}

几年前写asp代码的时候经常用这样的方法…

View变得简单的,Controller复杂了。

太依赖说View,会存在一些问题。假若哪天客户说按钮上的文字改为“通过审核”,或者是做个多语言版的,那就麻烦了。

参考:http://www.ervinter.com/2009/09/25/asp-net-mvc-how-to-have-multiple-submit-button-in-form/

方法三:使用ActionSelector

关于ActionSelector的基本原理可以先看下这个POST使用ActionSelector控制Action的选择。

使用此方法,我们可以将控制器写成这样:

以下为引用的内容:

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

在 View中:

以下为引用的内容:





此时,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);
}
}


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.