Calling the Controller Method

Asked

Viewed 7,584 times

3

I’m doing a project for the College, and I’m having a hard time understanding how to create a button and call an Actionresult in the Controller, and I was wondering if the action needs to be in the corresponding Model controller.

To better understand, this would be a view with 2 buttons, 2 actions in the controller and make a button call action1 and the other call action2

3 answers

4


I’m doing a project for the College, and I’m having a hard time understanding how to create a button and call an Actionresult in the Controller...

If we’re talking about a <input> of the kind submit (which is a kind of button), the right way is to create a <form> for him. In ASP.NET MVC using Razor, it works as follows:

@using (Html.BeginForm("MinhaAction", "MeuController", FormMethod.Get))
{
    <input type="submit" value="Ir para Action" 
         name="botao1" id="botao1" />
}

Or using the tag <button>:

@using (Html.BeginForm("MinhaAction", "MeuController", FormMethod.Get))
{
    <button type="submit" name="botao1" id="botao1">Ir para Action</button>
}

To work, your Controller need to have:

[HttpGet]
public ActionResult MinhAction()
{
    ...
}

..., and wanted to know if the action needs to be in the corresponding controller of the respective Model.

Not necessarily. A Model is not necessarily linked to a Controller. A Controller can work with 0, 1 or N Models.

To better understand, this would be a view with 2 buttons, 2 actions in the controller and make a button call action1 and the other call action2

It can be done like this:

@using (Html.BeginForm("MinhaAction1", "MeuController", FormMethod.Get))
{
    <button type="submit" name="botao1" id="botao1">Ir para Action 1</button>
}

@using (Html.BeginForm("MinhaAction2", "MeuController", FormMethod.Get))
{
    <button type="submit" name="botao2" id="botao2">Ir para Action 2</button>
}

Controller:

[HttpGet]
public ActionResult MinhAction1()
{
    ...
}

[HttpGet]
public ActionResult MinhAction2()
{
    ...
}
  • In the case of@using (Html.Beginform("Minhaaction", "Meucontroller", Formmethod.Get)) { <input type="Submit" value="Go to Action" name="botao1" id="botao1" /> }' how would I pass my object to the action? In case my project needs to have a buy button for each item, and the button is displayed by foreach and each item has the ID...

  • 1

    I suggest you ask a new question, now talking about this new need. It is a new answer, complementary to this.

2

In ASP.NET MVC the controller flame and through routes in case when you create a project in ASP.NET MVC you already come with a route created Home/Index which means that you will call Homecontroller and Index method which so it will direct you to your view in the folder Home/Index.cshtml that in the desired case and page so that it can direct to this page in case you want to create for example another action Home/about you would have to have view in the home/about.cshtml folder I will show with codes.

Below is how it looks in the code and how it was generated in html. have these three ways you can create links to redirect your pages you can use button but will always follow same rule.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 2

    It would be better for your answer not to use code images, but code that can be copied and pasted. Another thing are examples of Controller Actions.

1

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.1

  <form method="post" action="/Demo/RegisterInput">
   Email:
   <input type="email" data-val="true"
          data-val-email="The Email Address field is not a valid email address."
          data-val-required="The Email Address field is required."
          id="Email" name="Email" value="" /> <br>
   Password:
   <input type="password" data-val="true"
          data-val-required="The Password field is required."
          id="Password" name="Password" /><br>
   <button type="submit">Register</button>
 <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>" />

Browser other questions tagged

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