How to return several values of a class?

Asked

Viewed 295 times

1

Follows code:

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 for variable result ? Because the AspNetUser and AspNetUser tbm are classes created with ADO.NET Entity Data Model. I have tried making a ToArray or ToList and I couldn’t. I’m having trouble returning.

Can someone help me ?

  • 1

    These codes do not make much sense. It would be better if you put a real example that has doubt. If you only want to know how to return more than one value there is already an answer to this in https://answall.com/q/195460/101 and therefore it is duplicate. Or https://answall.com/q/94882/101 if you are not using C# 7. The exact solution depends on what you need.

  • Another example that may not be exactly the same more can be useful https://answall.com/a/80993/30045

  • 2

    The comment didn’t help much, but it seems to confirm what I already thought. I just can’t say if it’s a better solution because you don’t have enough information to make that decision. Or use tuples or a class as Brunno indicated.

1 answer

2


The correct way is to define a Viewmodel:

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

I mean, you can make a Helper thus (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 the 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.
}
  • Gypsy, what’s the helper for ?

  • In ASP.NET MVC, a Helper is a static class responsible for having a logic that is used several times throughout the application. For example, a search to the Post Office website can be implemented in a Helper. I can answer this in more detail in a question about Helpers in ASP.NET MVC, if desired.

  • Just open another question and paste the link here for me, so I’ll be notified.

Browser other questions tagged

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