ErocM
ErocM

Reputation: 4662

Pass variable on button click

I want to call the same class from different buttons. Here is what I am doing:

<div class="buttondiv">
  @using (Html.BeginForm("TankList","Forms", FormMethod.Post))
  {
    <input class="allformsbutton" type="submit" value="ASME Basic Form" id="buttonAsmeBasic" />
  }
</div>

<div class="buttondiv">
  @using (Html.BeginForm("TankList", "Forms", FormMethod.Post))
  {
    <input class="allformsbutton" type="submit" value="ASME Detailed Form" id="buttonAsmeDetailed" />
  }
</div>

I want to pass to the class that I'm calling "TankList" which button was clicked.

How would I capture that in the class?

EDIT

I wanted to clarify. The point of the buttons is to uniquely identify which button was pressed. So, I want to pass to the TankList the value "ASME Basic Form" if the ASME Basic button was pressed or pass "ASME Detailed Form" if the ASME Detailed button was pressed.

Upvotes: 0

Views: 555

Answers (1)

Daniel
Daniel

Reputation: 9849

your .cshtml-file:

<div class="buttondiv">
    @using (Html.BeginForm("TankList","Home", FormMethod.Post))
    {
        <input name="theClass" class="allformsbutton" type="submit" value="ASME Basic Form" id="buttonAsmeBasic" />
    }
</div>

<div class="buttondiv">
    @using (Html.BeginForm("TankList", "Home", FormMethod.Post))
    {
        <input name="theClass" class="allformsbutton" type="submit" value="ASME Detailed Form" id="buttonAsmeDetailed" />
    }
</div>

your FormsController.cs

public class FormsController : Controller
{
    [HttpPost]
    public void TankList(string theClass)
    {

    }
}

When you post the form the result in the controller is as follows depending on the button you clicked:

enter image description here enter image description here

Upvotes: 3

Related Questions