How to use 4 model in a view

Asked

Viewed 75 times

1

Hi, I’m 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 on just one page, where I load all the customers and select the client, then fill in the rental data, then select the bucket and already play directly in a table the bucket ID, name, price and total value, on the page itself, and at the end I save the data you have on Locacao and in the Item at the bank.

1 answer

2

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

Do it for relationships.

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

Locacao.Cs

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

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

2nd 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:

Clienteviewmodel.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 to look to understand better what I explained.

Browser other questions tagged

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