views:

139

answers:

4

I have one View and one Controller in my ASP.net MVC Application.

I have one "Submit" button & one "Cancel" button in my view.

When I click any of the botton below action will be triggered.

AcceptVerbs(HttpVerbs.Post)

public ActionResult Result()

{

}

Now the Question is how i will know in my public ActionResult Result() , whether post back caused because of "Submit" or "Cancel" ??

A: 

You could solve the problem with the following steps:

i. Place a hidden field called 'cancel' in the form on your view. Give it a default value of false.

ii. In the onclick event for your cancel button, add the following script:

document.getElementById('cancel').value = true; document.getElementById('myForm').submit();

Change 'myForm' to be the name of your form.

iii. Change the method signature for Result() to the following:

public ActionResult Result(bool cancel)

Through model binding, the value of your hidden field 'cancel' will be accessible in the signature of your action method.

pmarflee
A: 

you can try like

function OnBeginRequest(sender, args) {
        var postBackElement = args.get_postBackElement();
if ((postBackElement.id == '<%= btnSave.ClientID %>')
  {

  }
}
Muhammad Akhtar
A: 

Thanks for the answers. One more solution I got is ,

public ActionResult Result()
    {
        if (Request.Params.ToString().IndexOf("btnSubmit") > 0)
            //
        else
            //
    }
Nagendra Baliga
A: 

I would suggest that your "cancel" button should not submit the form. There are several ways to handle this, but the simplest would be to have a javascript onclick event on the button that navigates back to the previous page. (history.go(-1)) If that doesn't meet your needs, you can also specify a specific route to navigate to. Most complex would be to intercept the click on the client side and decide which route to hit, but I typically only do that if I want to have multiple submit buttons on the form that will hit different actions, and that's a fairly rare situation.

GalacticCowboy