当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET教程:网页表单多个按钮完成不同功能

ASP.NET
Asp.net 时间操作基类(支持短日期,长日期,时间差)
asp.net 获取机器硬件信息(cpu频率、磁盘可用空间、内存容量等)
asp.net 数据库备份还原(sqlserver+access)
Asp.Net 数据操作类(附通用数据基类)
Asp.net 弹出对话框基类(输出alet警告框)
Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)
Asp.net Socket客户端(远程发送和接收数据)
Asp.net 字符串操作基类(安全,替换,分解等)
Asp.Net数据输出到EXCEL表格中
asp.net Gridview里添加汇总行
asp.net UpdatePanel的简单用法
asp.net ajaxControlToolkit FilteredTextBoxExtender的简单用法
this connector is disabled错误的解决方法
sql事务应用积累
asp.net Page.Controls对象(找到所有服务器控件)
在asp.NET中字符串替换的五种方法
ASP.NET缓存方法分析和实践示例代码
asp.net 在DNN模块开发中遇到的resx怪问题
ASP.NET State service状态服务的问题解决方法
asp.net 结合mysql存储过程进行分页代码

ASP.NET教程:网页表单多个按钮完成不同功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-30   浏览: 176 ::
收藏到网摘: n/a

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

image

如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。

方法一:使用客户端脚本

比如我们在View中这样写:

以下为引用的内容:

<input type="submit" value="审核通过"  onclick='this.form.action="<%=Url.Action("Action1") %>";' />
<input type="submit" value="审核不通过"  onclick='this.form.action="<%=Url.Action("Action2") %>";'  />
<input type="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,会存在一些问题。假若哪天客户说按钮上的文字改为“通过审核”,或者是做个多语言版的,那就麻烦了。

参考: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中:

以下为引用的内容:

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

参考:http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

方法四、改进

Thomas Eyde就方法三的方案给出了个改进版:

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

原文地址:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html