Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 03, 2009 11:08 AM by fayaz_3e
Member
2 Points
9 Posts
Nov 03, 2009 07:47 AM|LINK
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" ??
Star
9386 Points
1751 Posts
Nov 03, 2009 09:55 AM|LINK
Hi,
You will get the object triggering the submit. I mean you will get it in form collection.
I modified your code such that the ActionResult is accepting FormCollection
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Result(FormCollection form) { if(form["Submit"] != null) { // Submit click } else if(form["Cancel"] != null) { // Cancel click } }
where submit and cancel are names of input buttons of type submit
Nov 03, 2009 10:49 AM|LINK
Thanks it worked for me. I got one more solution like
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Result() { if(Request.Params.ToString().IndexOf("btnSubmit") > 0) { // Submit click }
else
{ // Cancel click } }
Nov 03, 2009 11:08 AM|LINK
Why should you look for all params and finding with index of?? You can check for null. And I recommend it. You can directly get it from Form, if you are not passing form collection...
Request.Form["btnSubmit"] != null
Nagendra Bal...
Member
2 Points
9 Posts
how to check which event caused the post back in ASP.net MVC
Nov 03, 2009 07:47 AM|LINK
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" ??
fayaz_3e
Star
9386 Points
1751 Posts
Re: how to check which event caused the post back in ASP.net MVC
Nov 03, 2009 09:55 AM|LINK
Hi,
You will get the object triggering the submit. I mean you will get it in form collection.
I modified your code such that the ActionResult is accepting FormCollection
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Result(FormCollection form)
{
if(form["Submit"] != null)
{
// Submit click
}
else if(form["Cancel"] != null)
{
// Cancel click
}
}
where submit and cancel are names of input buttons of type submit
Nagendra Bal...
Member
2 Points
9 Posts
Re: how to check which event caused the post back in ASP.net MVC
Nov 03, 2009 10:49 AM|LINK
Thanks it worked for me. I got one more solution like
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Result()
{
if(Request.Params.ToString().IndexOf("btnSubmit") > 0)
{
// Submit click
}
else
{
// Cancel click
}
}
fayaz_3e
Star
9386 Points
1751 Posts
Re: how to check which event caused the post back in ASP.net MVC
Nov 03, 2009 11:08 AM|LINK
Why should you look for all params and finding with index of?? You can check for null. And I recommend it. You can directly get it from Form, if you are not passing form collection...
Request.Form["btnSubmit"] != null