A view using two MVC models

Asked

Viewed 1,815 times

-4

Good afternoon, everyone,

I have an MVC application and I need a view to access two models. The models are: inserir a descrição da imagem aqui

And each model has its controller, where I connect to Webservice to bring the data from another application. inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I need that in the Project view I can also use the model Detail Model, how can I do this? I’m new to MVC so I’m having a hard time getting it done...

  • 1

    Create a Viewmodel that contains your two models

  • You can use partial views.

  • Take it easy, first the actions are the same image. Second, it is not possible to call two action, you will need to use a Partial View or make a service layer.

  • First thing is to post the code in text and remove the images.

  • Related: https://answall.com/questions/110056/view-com-2-model-dentro-de-uma-viewmodel

  • Conceptually a view should link with a single controller. You can even do mirabolisms like the one you suggest, but it’s a one-way street, you start building problems above you and you get deeper and deeper. At the end your application has two paths works and will never receive maintenance (unviable) or does not work and your work will have been useless.

Show 1 more comment

1 answer

1

Just create a ViewModel with both models

public class DetalheProjetoViewModel
{
    public ProjetoModel Projeto { get; set; }
    public DetalhePrjModel Detalhe { get; set; }
}

and then change in the view

@model DetalheProjetoViewModel
  • I created the Viewmodel public class Detailsproject Viewmodel { public Detailsproject Detail { get; set; } public Project Project { get; set; } } Changed view Project for model Detailsproject except that when I do for, it displays the following message: Ienumerable does not contain a Definition for 'Project' and no Extension method 'Project' acceptin a first argument of type 'Ienumerable' could be found The view looks like this: foreach (var item in Model.Project) { Html.Displayfor(modelitem => item.Projeto.Code) }

  • @Emanuelebaron If you need a list on view you have to change the statement to @model IEnumerable<DetalheProjetoViewModel>

Browser other questions tagged

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