How to create a View to insert multiple entities

0

I have an entity that has child entities, for example: - Supplier (owns the main supplier data) - SupplierEmails - ProviderTelephones - SupplierEnderts - etc ...

Where each entity is a related database table.

In MVC I created a Model for each entity and there was a doubt.

To list, I will only use the main entity, however to insert a new record or edit an existing one, I will need my View to show all the fields of all entities.

What better solution? - Create a View for each entity, and somehow call all views on the same page? - or just create a view with all entities?

In both cases, how do you implement these solutions?

    
asked by anonymous 08.08.2017 / 20:53

2 answers

2

your view:

public class FornecedorViewModel 
{
    public FornecedorViewModel()
    {
        FornecedorEmails = new List<FornecedorEmailsViewModel>();
        FornecedorTelefones = new List<FornecedorTelefonesViewModel>();
        FornecedorEnderecos = new List<FornecedorEnderecosViewModel>();
    }
    //suas outras props aqui..

    public virtual IEnumerable<FornecedorEmailsViewModel> FornecedorEmails { get; set; }
    public virtual IEnumerable<FornecedorTelefonesViewModel> FornecedorTelefones { get; set; }
    public virtual IEnumerable<FornecedorEnderecosViewModel> FornecedorEnderecos { get; set; }
} 

For the list screen, if the screen is just a list of forwards, you should type your view as follows:

@model IEnumerable<FornecedorViewModel>

For screens where you will need only 1 Vendor, but with your child attributes, type as below:

@model FornecedorViewModel

Note that the differential will be where you get the data (in the case of a repository). You should only bring the Vendor data in the list screen, while in the other you should bring the Vendor and its daughter classes results.

To access the child properties of Vendor in your view, simply call Model (with M majusculo) and from there, you will navigate to any daughter entity. Example:

@Model.FornecedorEmails

Note: this is just a vision. You can implement N in different ways.

    
08.08.2017 / 22:43
0

Well, I would create a new model obterFornecedoresModel With the definitions of the other models. Type public IEnumerable<Fornecedor> Fornecedores{ get; set; } And so on with the other entities.

This would have only one view, with a model, and would do all the interactions in the controller. I do not know if it's the best way, but I think it helps.

    
08.08.2017 / 21:29