Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two buttons in my page and i want to invoke two different methods on each different button click.

HTML
@using (Html.BeginForm("DeleteConfirmed", "Thomson"))
{
    @Html.AntiForgeryToken()
    <div style="float:left;" class="panel123 col-sm-4">
      
            <div class="form-group">
                @Html.LabelFor(a=>a.UName)
                @Html.TextBoxFor(a => a.UName)
                @Html.ValidationMessageFor(a => a.UName)

            </div>
            <div class="form-group">
                @Html.LabelFor(a=>a.Password)
                @Html.PasswordFor(a => a.Password)
                @Html.ValidationMessageFor(a => a.Password)
            </div>
            <div align="center">
                <input type="submit" value="Submit" id="btnSubmit" class="btn-default" />
            </div>
        <div>
            <input type="submit" value="Delete" id="btnDelete" class="btn-default" />
        </div>
        @Html.ValidationMessage("CustomError")
        @Html.ValidationSummary(true)
    </div>


But i am unable to invoke another method how can i do achieve this?
Posted
Comments
phil.o 15-Sep-15 7:02am    
Please define "unable to invoke another method".
Thanks7872 15-Sep-15 7:45am    
I dont see even a single click event and not clear what is the problem here.

1 solution

Give your input buttons the same name;

XML
<input name="submit" .... />


then add a "name" attribute to your action

C#
public Action DeleteConfirmed(YourModelType model, string submit)
{
  // the value of "submit" will be "Submit" or "Delete" depending on which button was pressed
}
 
Share this answer
 
Comments
chandra sekhar 15-Sep-15 7:37am    
I want to invoke different methods in controller.As of now one button is hitting DeleteConfirmed method as i have mentioned but what about another method?
F-ES Sitecore 15-Sep-15 7:40am    
You can't easily call different actions, you'll need to call the relevant functions inside your action.

public Action DeleteConfirmed(YourModelType model, string submit)
{
if (submit == "Submit")
{
FunctionA();
}
else if (submit == "Delete")
{
FunctionB();
}

}
chandra sekhar 15-Sep-15 7:43am    
Is there any other way of doing this?
F-ES Sitecore 15-Sep-15 7:45am    
Yes, but they're the wrong way to do it, this is the right way.

Edit: This is an ok solution too, probably the best non-js\non-html5 answer

http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework
[no name] 15-Sep-15 9:59am    
Sir I have post an article http://www.codeproject.com/Articles/1030470/Working-with-Hadoop-Cluster-Set-up on Working with Hadoop Cluster Set up but you said it is copied and it was deleted.I have written that article my own.Please tell me from where I have copied..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900