2

i am new in mvc. i know this way i could handle multiple submit button in same form.

<%
 using (Html.BeginForm("LoginRegistration", new { action = "LoginRegistration", controller = "Register"}, FormMethod.Post, new { @class = "form", id = "formId" }))
    {
     %>
        //your html code here
        <input type="submit" name="submitButton" value="Login"/>
        <input type="submit" name="submitButton" value="Register"/>
<% }%>

[HttpPost]
        public ActionResult LoginRegistration(string submitButton)
        {
            switch (submitButton)
            {
                case "Login":
                    return RedirectToAction("Login");
                case "Register":
                    return RedirectToAction("Register");
                default:
                    return null;
            }
        }

in my case i have to give button name submitButton for all submit button but i want to give different name to all submit button. is it possible?

i like to know is there any other way to handle this situation. so please discuss all other way with sample code to handle this situation. thanks

UPDATE

i saw that another person dealing the same issue the below way. code is given but i do not understand his trick. he add one hidden field for one submit button...why? he set button value property to true and corresponding hidden field's value property to false...why i just could not understand this trick.

another issue is when action method will be called then how bool value will be pass and which control's bool value will pass....please go through this updated code and guide me how this code works in details. thanks

<%
 using (Html.BeginForm("LoginRegistration", new { action = "LoginRegistration", controller = "Register"}, FormMethod.Post, new { @class = "form", id = "formId" }))
    {
     %>
        //your html code here
        <button type="submit" name="login" value="true">Login</button>
        <input type="hidden" name="login" value="false"/>
        <button type="submit" name="register" value="true">Register</button>
        <input type="hidden" name="register" value="false"/>
<% }%>

[HttpPost]
        public ActionResult LoginRegistration(bool login, bool register)
        {
           if (login) return RedirectToAction("Login");
           else if (register) return RedirectToAction("Register");
        }

UPDATE

how my view look like

<div id="mydiv">
    @using (Html.BeginForm("MultipleSubmitBtn", "MultipleSubmitBtn", FormMethod.Post, new { @Id = "Form1" }))
        {
            <table border="0">

                <tr>
                    <td colspan="2">
                    <button type="submit" id="b1" value="true"> 1 </button>
                    <button type="submit" id="b2" value="true"> 2 </button>
                    <button type="submit" id="b3" value="true"> 3 </button>
                    <button type="submit" id="b4" value="true"> 4 </button>
                    <button type="submit" id="b5" value="true"> 5 </button>

                    <input type="hidden" id="b1" value="false" />
                    <input type="hidden" id="b2" value="false" />
                    <input type="hidden" id="b3" value="false" />
                    <input type="hidden" id="b4" value="false" />
                    <input type="hidden" id="b5" value="false" />
                    </td>
                </tr>
            </table>
        }
</div>

controller code

public class MultipleSubmitBtnController : Controller
    {
        [HttpPost]
        public ActionResult MultipleSubmitBtn(string b1, string b2, string b3, string b4, string b5)
        {
            return View();
        }

    }

when i use like this syntax then i got error

 public ActionResult MultipleSubmitBtn(string? b1, string? b2, string b3?, string b4?, string b5?) 

please check your code and tell me why it is not working. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626

3 Answers3

7

In HTML5 there's a new attribute named formaction where you can change the action attribute of the form depending on what button is being clicked.

<input type='submit' formaction='/home/login' value='Login'>
<input type='submit' formaction='/home/register' value='Register'>

However, this tag is isn't valid in IE9 and earlier.

Edit: extended answer

The idea behind the second example you're given is that a button only sends its value if it's being clicked. For the action method to work you need to have both a login and a register value being passed on. But since only one of the buttons can be pressed, only one of the values will be passed on. That's why he added two hidden fields with default value of false, thus making sure that both parameters always will be passed on to the action method. The button being clicked will override the hidden field.

The first variable pair has their name set to login. Notice that it's being defined twice, first as a button and then as a hidden field. If this button is clicked then true will be sent. If the other button is clicked then this button's value will not be sent and instead the hidden field (with the same name) will be sent.

<button type="submit" name="login" value="true">Login</button>
<input type="hidden" name="login" value="false"/>

The second variable pair (with the name Register) works in the same way. The hidden field becomes a "fallback option" in case this button isn't clicked.

<button type="submit" name="register" value="true">Register</button>
<input type="hidden" name="register" value="false"/>

Only one of the buttons can be clicked. If the register button is clicked then the fallback value for login will be used. If the login button is clicked then the fallback value for register will be used.

In total there are only two variables that will be sent to the action script: login and register.

If you want to solve it on the client side you can simply let the submit buttons change the action using the onclick event.

<form name="myFormName" action="Login">
  <input type="submit" value="Login" />
  <input type="submit" onclick="document.myFormName.action='Register'" value="Register" />

You need to use the name attribute in the form for this to work.

Edit 2: more on buttons when clicked

If you have 5 buttons...

<button type="submit" id="b1" value="true"> 1 </button>
<button type="submit" id="b2" value="true"> 2 </button>
<button type="submit" id="b3" value="true"> 3 </button>
<button type="submit" id="b4" value="true"> 4 </button>
<button type="submit" id="b5" value="true"> 5 </button>

...and you click on b3 then ONLY b3 will send it's value back to the server. This is just so you easily can see, on the server, what button that was clicked.

If you have an action method on your server that NEEDS all 5 button-variables then you have to make sure you'll send them in a different way. The reason for that is that MVC tries to match incoming variables with an action method and this action method needs all 5 button-variables to be valid.

[HttpPost]
public ActionResult LoginRegistration(string b1, string b2, string b3, string b4, string b5)
{
  ...
}

Since b1, b2, b4 and b5 won't be sent to the server when we click b3, and since the action method needs them, we have to figure out another way of sending them.

One way is to use hidden field that have the same name as the buttons. A hidden field is ALWAYS being sent to the server. So by adding these fields AFTER the buttons we can make sure that all of b1, b2, b3, b4 and b5 get something sent to the server. If we click on b3 then the value of the button will be used instead of the hidden field.

<input type="hidden" id="b1" value="false" />
<input type="hidden" id="b2" value="false" />
<input type="hidden" id="b3" value="false" />
<input type="hidden" id="b4" value="false" />
<input type="hidden" id="b5" value="false" />

By doing this way the action method above will work because no matter what button the user clicks, all five b-values will be sent to the server.

Edit 3 : without hidden fields It's possible to have different buttons without having fallback hidden fields that sends the value. We still consider the same buttons as before, with different names. But this time we have no hidden values. So if Button 3 is clicked then ONLY b3=true is sent to the server.

<button type="submit" id="b1" value="true"> 1 </button>
<button type="submit" id="b2" value="true"> 2 </button>
<button type="submit" id="b3" value="true"> 3 </button>
<button type="submit" id="b4" value="true"> 4 </button>
<button type="submit" id="b5" value="true"> 5 </button>

For the action method to work with this scenario we need to make all the fields optional so MVC can match all/some/no fields against incoming variables. It is done by writing string? instead of string in the parameter field. The? means optional (it actually means that the value can be empty/null).

[HttpPost]
public ActionResult LoginRegistration(string? b1, string? b2, string? b3, string? b4, string? b5)
{
  ...
}

So in this case this action method would work even though only b3=true is being sent and all the others are not.

Ohlin
  • 4,068
  • 2
  • 29
  • 35
  • thanks for your answer but i need a generic solution which works in all browser. – Thomas Sep 06 '13 at 07:06
  • You wrote "all other ways" so I wanted to present one of them. But I agree that HTML5 is a bit too "new" right now. – Ohlin Sep 06 '13 at 07:08
  • thanks for the answer.i like to know when form will be submit just clicking on any button then i like to know which field's value will pass to action method. there is found field in form two is button and two is hidden field but action method expect two argument which is not clear to me. i am looking forward for another explanation. thanks – Thomas Sep 06 '13 at 09:49
  • u said "Only one of the buttons can be clicked. If the register button is clicked then the fallback value for login will be used. If the login button is clicked then the fallback value for register will be used" what is fallback value means here? is it a general behavious of web apps or is it mvc specific that only hidden field values will go to action method? thanks for your answer but things is not getting clear that why only hidden field value will be send as parameter to action method. – Thomas Sep 06 '13 at 15:02
  • suppose i have one form with few textboxes and dropdown etc and when i will click on submit button then what are the values will pass to action method? – Thomas Sep 06 '13 at 15:03
  • It differs what will be sent back to the server. Text boxes and dropdown will send the value. Checkboxes will only send value if they're checked. Buttons will only send value if they're clicked. If you have five buttons then only one will be clicked. I'll update my answer with some more button examples. – Ohlin Sep 06 '13 at 15:12
  • @ Ohlin: i was reading this post and +1 from my side for your effort. – Mou Sep 06 '13 at 18:53
  • @Thomas All this about what value is being sent back to the server is only HTML in the Web browser, it has nothing to do with ASP.NET, Coldfusion or PHP. When I wrote about the Action method it was just how ASP.NET can receive what the browser is sending back. – Ohlin Sep 07 '13 at 08:32
  • sorry for late testing ur code but it did not work. please see the code which i test according to your guidance. i update my question. please have a look. thanks – Thomas Sep 16 '13 at 15:21
2

I know it is a bit late to answer still I am giving it a try.... :)

I believe that a more sophisticated way of doing it would be using the ActionNameAttribute and custom ActionMethodSelectorAttribute(deriving from ActionMethodSelectorAttribute). It is very easy to implement.

If you wish you can visit these links: http://programersnotebook.blogspot.in/2014/02/aspnet-mvc-actionnameselector-and.html, http://programersnotebook.blogspot.in/2014/02/aspnet-mvc-actionnameselector-and_2.html

Sayan Pal
  • 4,768
  • 5
  • 43
  • 82
0

Html:

<input type="submit" value="Save" name="commandSave" />
<input type="submit" value="Approve" name="commandApprove" />

ASP.Net Core Controller:

if (Request.Form.ContainsKey("commandSave")) // do saveMethod
else if (Request.Form.ContainsKey("commandApprove")) // do approveMethod
EminST
  • 59
  • 5