你如何处理ASP.NET MVC框架中的多个提交按钮?
是否有一些简单的方法来处理来自同一表单的多个提交按钮? 例:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" value="Send" />
<input type="submit" value="Cancel" />
<% Html.EndForm(); %>
任何想法如何在ASP.NET Framework Beta中执行此操作? 我所搜索的所有示例都有单个按钮。
这是一个基于干净的基于属性的解决方案,主要根据Maartin Balliauw的帖子和评论提供的多重提交按钮问题。
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }
    public string Argument { get; set; }
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var isValidName = false;
        var keyValue = string.Format("{0}:{1}", Name, Argument);
        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }
        return isValidName;
    }
}
剃刀:
<form action="" method="post">
 <input type="submit" value="Save" name="action:Save" />
 <input type="submit" value="Cancel" name="action:Cancel" />
</form>
和控制器:
[HttpPost]
[MultipleButton(Name = "action", Argument = "Save")]
public ActionResult Save(MessageModel mm) { ... }
[HttpPost]
[MultipleButton(Name = "action", Argument = "Cancel")]
public ActionResult Cancel(MessageModel mm) { ... }
更新:Razor页面看起来提供了相同的功能。 对于新的开发,可能会更好。
给你的提交按钮一个名字,然后检查你的控制器方法中提交的值:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>
张贴到
public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }
    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }
    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }
}
编辑:
要扩展这种方法来处理本地化的网站,请将消息隔离到其他地方(例如,将资源文件编译为强类型的资源类)
然后修改代码,使其工作如下:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="<%= Html.Encode(Resources.Messages.Send)%>" />
<input type="submit" name="submitButton" value="<%=Html.Encode(Resources.Messages.Cancel)%>" />
<% Html.EndForm(); %>
你的控制器应该是这样的:
// Note that the localized resources aren't constants, so 
// we can't use a switch statement.
if (submitButton == Resources.Messages.Send) { 
    // delegate sending to another controller action
    return(Send());
} else if (submitButton == Resources.Messages.Cancel) {
     // call another action to perform the cancellation
     return(Cancel());
}
你可以像上面提到的那样在动作中检查名字,但是你可能会考虑这是否是好的设计。 考虑行为的责任是一个好主意,而不是将这种设计与UI按钮名称等UI元素相结合。 所以考虑使用2个表单和2个操作:
<% Html.BeginForm("Send", "MyController", FormMethod.Post); %>
<input type="submit" name="button" value="Send" />
<% Html.EndForm(); %>
<% Html.BeginForm("Cancel", "MyController", FormMethod.Post); %>
<input type="submit" name="button" value="Cancel" />
<% Html.EndForm(); %>
此外,在“取消”的情况下,你通常只是没有处理表单,并将转到一个新的URL。 在这种情况下,你根本不需要提交表单,只需要一个链接:
<%=Html.ActionLink("Cancel", "List", "MyController") %>
上一篇: How do you handle multiple submit buttons in ASP.NET MVC Framework?
下一篇: How do you create a dropdownlist from an enum in ASP.NET MVC?
