Different action for different buttons from a single page in MVC

Performing Multiple Actions from a Single ASP.NET MVC Form. Supporting multiple submit buttons on an ASP.NET MVC view. MVC razor form with multiple different submit buttons. ASP.Net MVC Form with two submit buttons/actions. How to handle multiple submit buttons in mvc. asp.net mvc - Posting form to different MVC post action. how to use multiple submit button in mvc.

To post from different buttons on a single MVC page to different action methods, we need some extra code. We need to create an attribute class by inheriting ActionNameSelectorAttribute and override IsValidName method. We will use the button name to call any particular action on the basis of button name. It looks some thing very difficult but in reality it is more than easy.

Let's creat our attribute class:

public class HttpParameterActionAttribute : ActionNameSelectorAttribute
{
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
            return true;

        var request = controllerContext.RequestContext.HttpContext.Request;
        return request[methodInfo.Name] != null;
    }
}

Now say we have two action method on the same controller with name "Save" and "SaveAndPublish". We want two buttons called "Save" and "Save and Publish" which will call the corresponding action methods. Create your both action method according to your requirements and add the code, we need to add the attribute on these methods "HttpParameterAction" see the code:

[HttpParameterAction]
public ActionResult Save(MyModel model)
{
    // ...
}

[HttpParameterAction]
public ActionResult SaveAndPublish(MyModel model)
{
    // ...
}

Now time to decorate our HTML

@using (@Html.BeginForm())
{
    -------------------------------
    -------------------------------
    -------------------------------
    <input type="submit" name="Save" value="Save" />
    <input type="submit" name="SaveAndPublish" value="Save and Publish" />
}

Note: I have not used any parameter in above html code, if you will use the action name in BeginForm it will give error:

The current request for action 'Save' on controller type 'MyController' 
is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Save(MyApp.ViewModels.MyViewModel) 
                on type MyApp.Controllers.MyController
System.Web.Mvc.ActionResult SaveAndPublish(MyApp.ViewModels.MyViewModel) 
                on type MyApp.Controllers.MyController
Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • mvc
  • razor
  • attributes
  • asp.net
By Ali Adravi On 26 Apr, 15  Viewed: 3,427

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 34,840

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 96,842

How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden   On 30 Jun 2013  Viewed: 3,067

MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi   On 29 Jun 2013  Viewed: 6,010

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 22,346