How to use 4 model in a view

1
Hello, I have 4 class Cliente , Locacao , Item and Cacamba :

public class Cliente
{
  public Guid ClienteID { get; set; }
  public string Nome { get; set; }
  ...............
}

public class Locacao
{
   public Guid LocacaoID { get; set;}
   public Guid ClienteID {get;set;}
   public DateTime DataLocacao { get; set; }
   public DateTime DataEntrega { get; set; }
   public string Endereco { get; set; }
   ........
}

public class Item
{
    public Guid ItemID {get;set;}
    public Guid LocacaoID {get;set;}
    public Guid CaçambaID {get;set;}
    public int Quantidade { get; set; }
}

public class Cacamba    
{
       public Guid CacambaID{get;set;}
       public string NomeCacamba {get;set;}
       public string Descricao {get;set;}
       public decimal Preco {get;set;}
       ............
 }

What I wanted, if possible, is to be able to do it in just one page, where I load all the clients and select the client, then fill in the data of the location, then select the bucket and already play directly in a table the ID of the bucket, the name, the price and the total value, in the page itself, and at the end I save the data it has in Locacao and Item in the bank.     

asked by anonymous 13.06.2016 / 10:55

1 answer

2

You have two options (more indicated) to do this.

Do 1st for relationships.

All your Models are related, so you can get the properties of others just by "browsing" between them. It would look something like:

Locacao.cs

  @Html.EditorFor(model => model.Cliente.Nome)

This is the normal way, without creating anything. Remember that if you want to save a list of data, or select an item in a DropDown , you must adapt it to your model.

2º Do by ViewModel

You can create a ViewModel and put the items in it. There is no need, since they are related to each other, but it is a valid option as well. It would look something like this:

ClientViewModel.cs

var clienteViewModel = new ClienteViewModel{
    Cliente = New Cliente(),//preencha o cliente aqui
    Locacao = New Locacao(),
    Item = new Item(),
    Cacamba = new Cacamba()
};

return View(clienteViewModel);

Either of these ways will solve your problem. But depending on what you really want to accomplish, it will have to be suitable for your View .

This answer is more detailed , I advise you to take a closer look at what I explained.

    
13.06.2016 / 15:00