Call Action as parameter in Html.Beginform - ASP . NET MVC 5

Asked

Viewed 535 times

0

I have a code that queries a Webapi, writes to the database, and queries the database, in this application, a view that has 3 buttons, namely:

  • Consult Ws
  • Inserts BD
  • Comic book consultation

I am using @Html.Beginform to submit the button action and call the responsible Actionresult for each processing.

In passing parameters, only one action is allowed (obviously). How do I make you have "a decision-making" or an "if", so that you call the correct action by clicking on its respective button?

Would it be better to use another form type? Or any other forms for this?

In the code below the "Consultaws" is the parameterized action that "should change" when clicking on a button other than "btnCsWs".

    @using (@Html.BeginForm("ConsultaWs", "Home", FormMethod.Get))
    {
            <text>codigo do país</text>
            @Html.TextBoxFor(model => model.RestResponse.result.country)
            <text>codigo do estado</text>
            @Html.TextBoxFor(model => model.RestResponse.result.abbr)
          <button type="submit" name="btnCsWs">Consulta WS</button>
          <button type="submit" name="btnInsereDB">Insere DB</button>
          <button type="submit" name="btnCsDB">Consulta DB</button>
    } 
  • Mauricio, you can put an id on the button and make a Javascript call for when clicked change the form action

1 answer

1

In browsers that support HTML5, you can use the attribute formaction to the Ubmit button:

@using (@Html.BeginForm("ConsultaWs", "Home", FormMethod.Get))
{
        <text>codigo do país</text>
        @Html.TextBoxFor(model => model.RestResponse.result.country)
        <text>codigo do estado</text>
        @Html.TextBoxFor(model => model.RestResponse.result.abbr)
      <button type="submit" formaction="@Url.Action("CsWs", "Home")" name="btnCsWs">Consulta WS</button>
      <button type="submit" formaction="@Url.Action("InsereDB", "Home")" name="btnInsereDB">Insere DB</button>
      <button type="submit" formaction="@Url.Action("CsDB", "Home")" name="btnCsDB">Consulta DB</button>
}

This will cause when giving Submit in the form, the request goes pro formaction of the button clicked.

In browsers that do not support HTML5, you can add an Event Listener by clicking a type button submit (you can improve the selector by delegating from a specific form, for example):

$(document).on('click', 'button[type="submit"]', function (event) {
  var action = $(this).attr('formaction');
  $(this).closest('form').attr('action', action);
});

Javascript will increase support for non-Html5 browsers, but if any user has javascript disabled and the browser is HTML5, the first option will allow it to work the way you need it to.

So it’s okay to use both options at the same time :)

  • Phiter, thanks for your help, I made the first choice and served very well. Already the second, I did not understand very well how to do, can explain me better kindly?

  • The second option, as I said, is for browsers that do not have native attribute support formaction. What happens is that you have placed a delegate click event for all buttons of type Submit. When such a button is clicked, it will change the action of the parent form to what is on its own formaction, that before the parent form Ubmit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.