Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle multiple submit buttons in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 612
    Comment on it

    "Handling multiple submit buttons in Asp.Net MVC"

        In various cases we have to take multiple submit buttons in a form , I will not discuss the cases but let us take the following example:

    <form method="post" action="/MultipleSubmit/PostData">
        <input type="text" name="Name" />
        <input type="submit" name="Cancel" value="Cancel" />
        <input type="submit" name="Delete" value="Delete" />
    
    </form>
    

       In the above View there are two submit buttons with name Cancel and Delete. The form action is set to "MultipleSubmit/PostData" (i.e Controller= "MultipleSubmit" and Method="PostData").

      In the MultipleSubmit controller we have a method:

        [HttpPost]
        public void PostData()
        {
    
        }        
    

      This means that either we click Cancel or Delete the same Action Method (PostData()) will be invoked.

    So what if we have to do something different on click of both the buttons.

    There can be many methods for this solution, but the method I followed is as follows:
    What I did is I created two different Methods for each submit button:

        [HttpPost]
        [ActionName("PostData")]
        public void Cancel()
        {
           // Do Something...
        }
    
        [HttpPost]
        [ActionName("PostData")]
        public void Delete ()
        {
           // Do Something...
        }
    

    This will not work, rather will give an error as both methods have the same ActionName. But now the question is how to invoke Cancel method onclick of the Cancel button and the Delete method onclick of the Delete button ?

    For this you have to create a class say MutilpeAttribute class and inherit the ActionMethodSelectorAttribute class. Now override the IsValidForRequest() method of the ActionMethodSelectorAttribute class as follows:

    public class MutilpeAttribute:ActionMethodSelectorAttribute
        {
          public string buttonName { get; set; }
          public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
            {
                return controllerContext.Controller.ValueProvider.GetValue(buttonName)!=null;
            }
        }
    

    The ControllerContext parameter in the IsValidForRequest() method contains the FormValueProvider object that contains values posted in the form in a NameValueCollection object. This will help us to check which button has submitted the form.

      Now place the attributes ([Multiple(buttonName="Cancel")], [Multiple(buttonName ="Delete")]) over the corresponding methods created in the MultipleSubmit controller,so as to map the submit buttons with their corresponding methods:

      [HttpPost]
        [Multiple(buttonName="Cancel")]
        [ActionName("PostData")]
        public void Cancel()
        {
    
        }
    
        [HttpPost]
        [Multiple(buttonName = "Delete")]
        [ActionName("PostData")]
        public void Delete ()
        {
    
        }
    

    These attributes will check, which button has submitted the form, and hence the corresponding method will be invoked.
    Hope this gives you an idea of handling multiple submit buttons..!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: