How to send 2 Controller objects to the View in C#?

Asked

Viewed 6,925 times

10

I made a query in the database in 2 tables and saved in 2 objects in the controller, but I can only send 1 object to the view.

// Controller
// Pego os dados do BD e salvo no objeto
var objPessoa = new PessoaAplic();
var dadosPessoa = objPessoa.ListarPorId(id);

// Pego os dados do BD e salvo no objeto
var objContato = new ContatoAplic();
var dadosContato = objContato.ListarPorId(dadosPessoa.IdPessoa);

return View(dadosPessoa);

I’m sending the object dadosPessoa to the view, more precise to send the dadosContato also?

How do I send the 2 objects to View?

  • Really what returns the Listarporid of the two classes ? So the answer is correct your question! (What kind of data do they return)

3 answers

9

Create a Viewmodel:

public class PessoaContatosViewModel
{
    public PessoaAplic Pessoa { get; set; }
    public ContatoAplic Contato { get; set; }
}

Use:

// Controller
// Pego os dados do BD e salvo no objeto
var viewModel = new PessoaContatosViewModel {
    Pessoa = objPessoa.ListarPorId(id),
    Contatos = objContato.ListarPorId(dadosPessoa.IdPessoa)
};

return View(viewModel);

Don’t forget to modify the View:

@model MeuProjeto.ViewModels.PessoaContatosViewModel

9


You can send via Viewbag, Viewdata or make a class that contains the information (list, item) to be consumed in your View (ViewModel)

You can use any of the 3 correctly, for example, load a Dropdownlist is very simple with Viewbag. If you are going to pass many class objects and want to keep all this standard you can use a Viewmodel that represents all these class objects.

But, I prefer to pass all the ways for you to know and use properly ...

Viewbag

public ActionResult View(int id)
{
    var objPessoa = new PessoaAplic();
    var dadosPessoa = objPessoa.ListarPorId(id);

    ViewBag.DadosPessoa = dadosPessoa;

    var objContato = new ContatoAplic();
    var dadosContato = objContato.ListarPorId(dadosPessoa.IdPessoa);

    ViewBag.DadosContato = dadosContato;

    return View();
}

Viewdata

public ActionResult View(int id)
{
    var objPessoa = new PessoaAplic();
    var dadosPessoa = objPessoa.ListarPorId(id);

    ViewData.Add("DadosPessoa",dadosPessoa);

    var objContato = new ContatoAplic();
    var dadosContato = objContato.ListarPorId(dadosPessoa.IdPessoa);

    ViewData.Add("DadosContato",dadosContato);

    return View();
}

Viewmodel

Class responsible for dealing both information:

public class ViewModel

{
    public ViewModel(PessoaAplic pessoa, ContatoAplic contato)
    {
        Pessoa = pessoa;
        Contato = contato;

    }

    public PessoaAplic Pessoa { get; private set; }
    public ContatoAplic Contato { get; private set; }
}

Solution within the Controller

public ActionResult View(int id)
{
    var objPessoa = new PessoaAplic();
    var dadosPessoa = objPessoa.ListarPorId(id);

    var objContato = new ContatoAplic();
    var dadosContato = objContato.ListarPorId(dadosPessoa.IdPessoa);

    ViewModel viewModel = new ViewModel(dadosPessoa, dadosContato);

    return View(viewModel);
}

Views:

Viewbag

@{
    Layout = null;

    WebApplication2.Models.PessoaAplic Pessoa = (WebApplication2.Models.PessoaAplic)ViewBag.DadosPessoa;
    WebApplication2.Models.ContatoAplic Contato = (WebApplication2.Models.ContatoAplic)ViewBag.DadosContato;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>
    <div> 
    </div>
</body>
</html>

Viewdata

@{
    Layout = null;

    WebApplication2.Models.PessoaAplic Pessoa = (WebApplication2.Models.PessoaAplic)ViewData["DadosPessoa"];
    WebApplication2.Models.ContatoAplic Contato = (WebApplication2.Models.ContatoAplic)ViewData["DadosContato"];
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>
    <div> 
    </div>
</body>
</html>

Viewmodel

@model WebApplication2.Models.ViewModel
@{
    Layout = null;

    WebApplication2.Models.PessoaAplic Pessoa = Model.Pessoa;
    WebApplication2.Models.ContatoAplic Contato = Model.Contato;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>
    <div> 
    </div>
</body>
</html>
  • I am particularly against approaches ViewBag and ViewData for being dynamic and allowing anti-patterns between layers. + 1.

  • 1

    @Romaniomorrisonmendez particularly speaking the 3 approaches are correct and the question refers how I should proceed in the passage of values, so I believe we should pass on any and all information so that the user nor the issue are limited to our cons. My intention was to show all types still has the Temdata that is not the case ...

4

You have to encapsulate them in another object. You can do this in several ways, one of them is to create a viewmodel:

public class PessoaViewModel {
    public Pessoa Pessoa {get; set;}
    public Contato Contato {get; set;}
}

Then you pass:

return View(new PessoaViewModel {Pessoa = dadosPessoa , Contato = dadosContato});

I put in the Github for future reference.

The kind of properties I kicked, since it was used a var in the code and I have no way of knowing what is the result of the operations performed. Adapt to the type required.

  • Really this ease to use var hinder the knowledge. People should rethink and not use var data that has type. + 1

  • @Cezar the var in itself is not bad, but there is a place where it should not be used, and that is precisely where it used. There is no way to know which type the method returns without being in the IDE: http://answall.com/q/47383/101

  • 1

    That’s what you said there is a place to use correct typing, including code legitimacy!!! it would be much better to use your type, I even asked him to send the guys if yes I change the question so that the question is correct!

Browser other questions tagged

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