How to return several values of a class?

1

Follow the code below:

Controller:

public ActionResult teste1(int num1, int num2)
{
  // Aqui quero obter os valores da variável model1 e model2  da classe MyClass
  var result = MyClass.Teste2(num1 , num2);
}

Model:

public class MyClass
{
  public static Teste2(int num1, int num2)
  {
     var model1 = new AspNetUser();
     var model2 = new AspNetUser1();

     using (var ctx = new Entities())
     { 
        model1 = ctx.Table.Where( x => ...).FirstOrDefault();
        model2 = ctx.Table.Where( x => ...).FirstOrDefault();
     }

     //aqui: como retornar com "model1" e "model2" para ação "ActionResult" onde tem variável "result"?
  }    
}

How can I return with variables model1 and model2 to variable result ? Because AspNetUser and AspNetUser tbm are classes created with ADO.NET Entity Data Model. I already tried to ToArray or ToList and could not. I'm having a hard time returning.

Can anyone help me?

    
asked by anonymous 03.05.2017 / 04:23

1 answer

2

The correct way is to define a ViewModel :

public class UsuariosViewModel
{
    public AspNetUser Model1 { get; set; }        
    public AspNetUser1 Model2 { get; set; }
}

That is, you can do a Helper as Model is something else):

public static class MyClass
{
  public static UsuariosViewModel Teste2(int num1, int num2)
  {    
     using (var ctx = new Entities())
     { 
        return new UsuariosViewModel
        {
            Model1 = ctx.Table.FirstOrDefault(x => ...),
            Model2 = ctx.Table.FirstOrDefault(x => ...)
        };
     }
  }    
}

And the return to View :

public ActionResult teste1(int num1, int num2)
{
  // Aqui quero obter os valores da variável model1 e model2  da classe MyClass
  var result = MyClass.Teste2(num1 , num2);
  return View(result); // result será UsuariosViewModel.
}
    
03.05.2017 / 04:50