Why do you want multiple ActionResults?? In the same ActionResult, you can check which submit causes postback and based on that you can do ur logic or return some view. Like
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test(FormCollection form)
{
if(form["btnA"] != null)
{
btnA is clicked. Call a method or return any View View("a");
}
else if(form["btnB"] != null)
{
btnB is clicked
}
}
You can check any condition and implement your logic....
I agree with @fayaz_3e in that you don't need multiple ActionResults and you don't need 3 forms on the page. You can do it all with 1 form and 1 action results like so:'
Notice all Three buttons have the same name, but different values. Depending on which button you click, that value is sent. So you can make your Action method look like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult action(string btnSubmit){
if(btnSubmit == "Button1"){
// Button1 was clicked
}
else if(btnSubmit == "Button2"){
// Button 2 was clicked
}
else if(btnSubmit == "Button3"){
// Button 3 was clicked
}
}
Note: In the action method I'm only passing the button, you probably want to also include a parameter for your model, other form values or the FormCollection.
tj.valiyamat...
Member
1 Points
5 Posts
How to use multple ActionResult into a single cotroller?
Dec 10, 2009 11:36 AM|LINK
I have three submit buttons in my View, Now I need to call the corresponding ActionResult by clicking on a button in the view.
Thanks in advance.
Aquaren
Contributor
2360 Points
421 Posts
Re: How to use multple ActionResult into a single cotroller?
Dec 10, 2009 11:44 AM|LINK
Each button can be wrapped in its own Html.BeginForm method and call a different action such as:
<% using (Html.BeginForm("action1", "ControllerName")) { %> <input type="submit" value="Button 1" /> <%} %> <% using (Html.BeginForm("action2", "ControllerName")) { %> <input type="submit" value="Button 2" /> <%} %> <% using (Html.BeginForm("action2", "ControllerName")) { %> <input type="submit" value="Button 3" /> <%} %>MVC Html.BeginForm
fayaz_3e
Star
9332 Points
1744 Posts
Re: How to use multple ActionResult into a single cotroller?
Dec 10, 2009 12:07 PM|LINK
Why do you want multiple ActionResults?? In the same ActionResult, you can check which submit causes postback and based on that you can do ur logic or return some view. Like
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Test(FormCollection form) { if(form["btnA"] != null) { btnA is clicked. Call a method or return any View View("a"); } else if(form["btnB"] != null) { btnB is clicked } }You can check any condition and implement your logic....
CodeHobo
All-Star
18647 Points
2647 Posts
Re: How to use multple ActionResult into a single cotroller?
Dec 10, 2009 05:43 PM|LINK
I agree with @fayaz_3e in that you don't need multiple ActionResults and you don't need 3 forms on the page. You can do it all with 1 form and 1 action results like so:'
The View:
<% using (Html.BeginForm("action", "ControllerName")){ %> <input type="submit" name="btnSubmit" value="Button1" /> <input type="submit" name="btnSubmit" value="Button2" /> <input type="submit" name="btnSubmit" value="Button3" /> <%} %>Notice all Three buttons have the same name, but different values. Depending on which button you click, that value is sent. So you can make your Action method look like this:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult action(string btnSubmit){ if(btnSubmit == "Button1"){ // Button1 was clicked } else if(btnSubmit == "Button2"){ // Button 2 was clicked } else if(btnSubmit == "Button3"){ // Button 3 was clicked } }Note: In the action method I'm only passing the button, you probably want to also include a parameter for your model, other form values or the FormCollection.
Blog | Twitter : @Hattan
tj.valiyamat...
Member
1 Points
5 Posts
Re: How to use multple ActionResult into a single cotroller?
Dec 14, 2009 03:25 AM|LINK
Thanks all. This is really very helpful