Calling Controller Method

3

I'm doing a project for the Faculty, and I'm having a hard time understanding how to create a button and call an ActionResult on the Controller, and wanted to know if the action needs to be on the corresponding controller of the respective Model.

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

    
asked by anonymous 28.10.2016 / 04:32

3 answers

4
  

I'm doing a project for College, and I'm having a hard time understanding how to create a button and call an ActionResult on Controller ...

If we are talking about a <input> of type submit (which is a type of button ), the correct way is to create a <form> for it. 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 by using the <button> tag:

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

In order to work, your Controller needs to have:

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

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

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

  

To understand better, it would be like this, a view with 2 buttons, 2 actions in the controler 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()
{
    ...
}
    
28.10.2016 / 05:05
2

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

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

    
28.10.2016 / 05:07
1

link

  <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>" />

    
10.11.2018 / 03:42