7

I have form, with two buttons:

 <form action="/Book/Crud" method="post">
        <input type="text" name="bookInput" />  
        <button type="submit">Save</button>
        <button type="submit">Save and exit</button>
    </form>

How can I pass to my controller, or, whatever, additional value, which depends on button?
<button type="submit" ?????? >Save</button> What should be here?

I am using Asp.Net Core, with this controller:

public IActionResult Crud(string bookInput)
{
    //removed code for brevity
}
Yurii N.
  • 5,455
  • 12
  • 42
  • 66

6 Answers6

5

You can use jquery to solve the issue:

cshtml

<form action="/Book/Crud" method="post">
        <input type="text" name="bookInput" />  
        <button type="submit" name="Submit">Save</button>
        <button type="submit" name="SubmitAndExit">Save and exit</button>
</form> 

jquery

$("form").submit(function () {
     var btn = $(this).find("input[type=submit]:focus" );
     var btnName = btn.attr('name');
     var input = $("<input>")
           .attr("name", "btnName").val(btnName);
     $(this).append(input);
     return true;
});
adem caglin
  • 22,700
  • 10
  • 58
  • 78
4

The answer of Bobby Jack is correct. I will elaborate on this answer in order to show how to access this input from within the controller. If you would use the following HTML:

<form action="/Book/Crud" method="post">
    <input type="text" name="bookInput" />  
    <button name="action" value="save" type="submit">Save</button>
    <button name="action" value="save-exit" type="submit">Save and exit</button>
</form>

You could reach the values: 'save' and 'save-exit' by defining another parameter in the controller like this:

// The parameter 'action' is what will contain the values specified in the button.
public IActionResult Crud(string bookInput, string action)
{
    // Implementation
}

The name of the parameter needs to correspond to the value in the 'name' attribute of the form button (in this case 'action'). When the button is clicked, ASP.net will automatically assign the value of the 'value' attribute to the controller parameter. Or more specifically the controller parameter 'action' will have a value of 'save' or 'save-exit' retrieved from the attribute 'value'.

Fluous
  • 2,075
  • 2
  • 17
  • 29
3
<form action="/Book/Crud" method="post">
    <input type="text" name="bookInput" />  
    <button name="action" value="save" type="submit">Save</button>
    <button name="action" value="save-exit" type="submit">Save and exit</button>
</form>

Like other inputs, the button can have a name and value attribute which will be submitted to your backend on submit. Only the button that was clicked will send its value. You need to use whatever mechanism you're using to access the POSTed variable named action (in this example). Then just switch on whether it's equal to save or save-exit and perform the relevant function.

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
  • The same question: how can I catch difference between those two buttons on server side? – Yurii N. Nov 22 '17 at 07:13
  • @YuriyN. Look at the value of the POSTed variable named action. If it's "save", the "Save" button was pressed. If it's "save-exit", the "Save and exit" button was pressed. – Bobby Jack Nov 23 '17 at 09:31
  • I'm not sure how can I do this at ASP.NET Core, could you explain please? – Yurii N. Nov 23 '17 at 09:35
  • I don't know asp.net, I'm afraid. Are you not already processing the 'bookInput' value? It should be the exact same procedure. – Bobby Jack Nov 23 '17 at 10:13
  • Yes, it is already processed, but this processing is already built-in ASP.NET Core, about processing `button`'s value, I'm not sure. – Yurii N. Nov 23 '17 at 10:27
  • I'm sure you'll be able to find the answer to "How do I access a POST variable in asp.net" on this site. E.g. https://stackoverflow.com/questions/4360956/how-to-get-post-and-get-data-from-a-form-in-asp-net – Bobby Jack Nov 23 '17 at 14:40
0

add different names

<button type="submit" name="save">Save</button>
<button type="submit" name="saveExit">Save and exit</button>
Kintamasis
  • 265
  • 6
  • 27
0

Example from VB.net:

<asp:Button id="cmdKeyphraseAdd" runat="server" Text="Add Keyphrase" onclick="cmdKeyphraseAdd_Click"></asp:Button>
Atrix
  • 299
  • 2
  • 6
-1

 <form action="/Book/Crud" method="post">
        <input type="text" name="bookInput" />  
        <button type="submit">Save</button>
        <button type="submit">Save and exit</button>
    </form>
fghfgh
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 22 '23 at 10:16