Home >Backend Development >C++ >How to Handle Multiple Submit Buttons in ASP.NET MVC Forms?

How to Handle Multiple Submit Buttons in ASP.NET MVC Forms?

DDD
DDDOriginal
2025-02-01 10:51:09236browse

How to Handle Multiple Submit Buttons in ASP.NET MVC Forms?

Process multiple submission buttons in ASP.NET MVC

This article discusses how to manage multiple submission buttons in the same form. Consider the following HTML code, which defines a form that contains two submission buttons "Send" and "Cancel":

<code>(此处应插入包含两个提交按钮的表单HTML代码示例)</code>
Generally, when processing the form in the ASP.NET MVC, the framework rely on the name of the button to determine the associated operation method to be executed. However, in the case of multiple buttons above, this method will become tricky.

In order to solve this problem, this article proposes a custom attribute called

. This attribute is used to intercept the processing process of operation and evaluate whether the requests that are introduced are derived from a specific submission button. If you find the matching item, identify and call the corresponding operation method. MultipleButtonAttribute

<code class="language-csharp">[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)
    {
        bool isValidName = false;
        string keyValue = string.Format("{0}:{1}", Name, Argument);
        ValueProviderResult value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }

        return isValidName;
    }
}</code>
Accept two parameters:

and MultipleButtonAttribute. Name The attribute is used to identify the name of the submit button, and the Argument attribute represents the value associated with the button. Name Argument To use , the HTML code of the form should be modified to contain the attribute name and parameter value. For example:

MultipleButtonAttribute In the controller, the corresponding operation method should be decorated with

, and the correct
<code>(此处应插入修改后的包含属性的表单HTML代码示例)</code>
and

value: MultipleButtonAttribute Name Argument Using this method, the ASP.NET MVC framework can now accurately map the submissions to the appropriate operation method, so as to correctly handle multiple submission buttons in the same form.

The above is the detailed content of How to Handle Multiple Submit Buttons in ASP.NET MVC Forms?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn