0

This is my first time working on a .net project and I am a bit confused on how to connect the html form with a controller class based on what action the user is taking in the html form.

cshtml form

<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="DecodeFiles">
    <div>
        <input type="file" name="files" id="inputFile">
        <button type="submit" class="btn btn-success btn-lg btn-block">Verify</button> // Set different piece of code in the controller method
        <button type="submit" class="btn btn-primary btn-lg btn-block">Dispense</button> // Set different piece of code in the controller method
    </div>
    <div>
        @ViewData["TextAreaResult"]
    </div>
</form>

controller method

[HttpPost("DecodeFiles")]
public IActionResult DecodeFiles(ICollection < IFormFile > files) {
    ViewData["TextAreaResult"] = "No result.";

    try {

        Control.Initialize();
        Control control = new Control();
        Request request = new Request();
        request.setRequestType(1);

        request.setCommandStatusCode(0); // Set only in case verify button is clicked
        request.setCommandStatusCode(1); // Set only in case dispense button is clicked

        ViewData["TextAreaResult"] = string.Format(" Response {0}", request.getHttpInformation());

    } catch (Exception exc) {
        ViewData["TextAreaResult"] = "Exception: " + exc.Message;
    }

    return View("Index");
}

How should I conditionally call

request.setCommandStatusCode(0); in case verify button is clicked request.setCommandStatusCode(1); in case dispense button is clicked

0xburned
  • 2,625
  • 8
  • 39
  • 69
  • 1
    Long time I haven't done this, but here where I would look first: button tag can have a name and a value attributes. Actually, it looks like this may be answered here: https://stackoverflow.com/questions/5438216/how-do-i-post-button-value-to-php – TTT Aug 02 '17 at 09:50

2 Answers2

2

If you change the action parameters to

public IActionResult DecodeFiles(int? button, ICollection < IFormFile > files)

and then in your cshtml

<button type="submit" name="Button" value="0" class="btn btn-success btn-lg btn-block">Verify</button> 
<button type="submit" name="Button" value="1" class="btn btn-primary btn-lg btn-block">Dispense</button>

you should get the value of the submitting button in your controller

ste-fu
  • 6,879
  • 3
  • 27
  • 46
0

This is already answered.

Hossein Margani
  • 1,056
  • 2
  • 15
  • 35