How to call another action and return the value?

Asked

Viewed 39 times

2

Follows code:

public ActionResult Teste1(int num1, int num2)
{
   var valor = Teste2(1, 2); //Aqui recebe valor nulo
}

public ActionResult Teste2(int num11, int num22)
{
   //Alguns valores aqui...
   var valor = 123;
   return null;
}

The values that are in the action Teste2, move on to action Teste1 variable valor, this is possible ?

1 answer

2


The values that are in the Teste2 action, move to Teste1 action with variable value, this is possible?

Possible, it is, but this is bad practice.

Note that the return of Teste1 and of Teste2 is a ActionResult, that is, a complete result of an action executed in Controller. So it just wouldn’t be bad practice if the idea were something like:

public ActionResult Teste1(int num1, int num2)
{
   return Teste2(1, 2); // veja este retorno.
}

public ActionResult Teste2(int num11, int num22)
{
   //Alguns valores aqui...
   var valor = 123;
   return Content(valor); // veja aqui também.
}

If the idea is to repurpose logic, state Helpers (static classes with static methods inside), which are more efficient and the return is in variables that you define.

  • This is the most correct way or is there another way ?

  • 1

    The one demonstrated in the example of the answer is the most correct one, including, regarding inheritance, which can also be used in this way.

Browser other questions tagged

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