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

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

Barbara Streisand
Barbara StreisandOriginal
2025-02-01 10:36:09294browse

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

Multiple submission buttons in the ASP.NET MVC framework

ASP.NET CORE MVC framework allows to use multiple submission buttons in a single form. This provides the flexibility of different operations based on the click button.

One way to handle multiple submission buttons is the attribute -based route. The following is an example:

Attribute:

Razor View:

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

(Here we should include the actual RAZOR code, displaying forms that contain two submission buttons, "Save" and "Cancel". Since the original text is not provided, it cannot be added here.) Controller:

Through this settings, the "Save" button is submitted to the "Save" operation, and the "Cancel" button is submitted to the "Cancel" operation. Update of Razor Pages:

<code class="language-csharp">[HttpPost]
[MultipleButton(Name = "action", Argument = "Save")]
public ActionResult Save(MessageModel mm) { /* 保存操作 */ }

[HttpPost]
[MultipleButton(Name = "action", Argument = "Cancel")]
public ActionResult Cancel(MessageModel mm) { /* 取消操作 */ }</code>

In Razor Pages, the same features are provided with land when opening the box. You can use the following syntax without using attributes:

(here it should include the actual Razor Pages code, displaying forms that contain two submit buttons, "Save" and "Cancel", and how to pass the instructions. Since the original text is not provided, it cannot be added here.)

In your page processing program, you can access the button value submitted by the "submit-button" key in the form set.

The above is the detailed content of How to Handle Multiple Submit Buttons in ASP.NET MVC?. 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