Input Submit does not call Actionresult

Asked

Viewed 434 times

2

I’m having trouble calling Actionresult using input type="submit" and pass the model data.

I did a test using ActionLink but Model is not sent.

Someone could shed a light?

Here is the code: Controller:

[HttpPost]
public ActionResult Aprovar(FormularioModel model)
{
//Código
}
[HttpPost]
public ActionResult Devolver(FormularioModel model)
{
//Código
}

View:

 @using (Html.BeginForm(null, "Domiciliacao", FormMethod.Post, new { id = "formFila" })) 
    {
@Html.Partial("~/Views/Shared/ViewPartial.cshtml", Model) //PartialView sendo carregada para complentar os dados da tela.
//Código
    <div>               
                    <input type="submit" value="Aprovar" name="Aprovar" formmethod ="post" formaction="~/Domiciliacao/Aprovar" class="btn btn-success btn-lg"/>
                </div>
                <div>                
                    <input type="submit" value="Devolver" name="Devolver" formmethod ="post" formaction="~/Domiciliacao/Devolver" class="btn btn-danger btn-lg"/>
                </div>
    }
  • Tried to name the action?

  • This is ASP.NET Core?

  • Aline: yes I did, but I have more than one action. - Jbueno: MVC4

  • So how do you define which action you’re sending?

  • Through the formaction

  • Yes, I saw him there. But you assign the attribute value of the pro form button at what point? With js?

  • Sorry, I don’t understand. I’m beginner in WEB development.

  • Don’t you have a code that assigns the attribute value: formaction para action to the form? All your code is the one you posted?

  • It’s not all, but if you’re referring to some <script>, I don’t have it in this view. I want to call Action passing the whole Model, with the changes that were made.

Show 4 more comments

2 answers

1

There are some ways to do this.

I changed some things in HTML:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formFila" })) 
{
    @Html.Partial("~/Views/Shared/ViewPartial.cshtml", Model){
        <div>               
           <input type="button" value="Aprovar" name="Aprovar" data-formaction="~/Domiciliacao/Aprovar" class="btn btn-success btn-lg btn-submit"/>
        </div>
        <div>                
           <input type="button" value="Devolver" name="Devolver" data-formaction="~/Domiciliacao/Devolver" class="btn btn-danger btn-lg btn-submit"/>
        </div>
    }
}

I used jquery for you to see:

$(document).ready(function(){
   $("#formFila .btn-submit").click(function(){
      var form = $("#formFila");
      form.attr("action", $(this).data("formaction");
      form.submit();
   });
});

If you’re using with jquery, don’t forget the lib reference: https://code.jquery.com/jquery-3.0.0.js

Or, as already suggested, you can make two presses. One for each situation.

  • Thanks Aline, I believe that the best way would be to use the same Script. Create two presses in my case would be unfeasible. I will test and put the return.

0

beginForm uses two parameters: Action and Controller. Example

@using (Html.BeginForm("SuaAction", "SeuController", FormMethod.Post, new { @role = "form", @id = "frm", enctype = "multipart/form-data" }))
{

}
  • Netinho, for cases where there is more than one action. In case I have two actions, Approve and Return.

  • Make two Beginform ones for each form. If you want to use the same for form for different actions depending on a conditional I recommend using Jquery.

  • But to send the entire Model via Jquery? how do you, I do not know?

  • Take a look at this post, it will give you a clarification. http://stackoverflow.com/a/42060555/4312593

Browser other questions tagged

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