Daniela
Daniela

Reputation: 657

Return 2 lists from one model to a view in MVC

I am new to MVC, and I am struggling with viewmodels. I would like to return 2 lists from the same model into one view. I will need 2 foreach loops to show records of a different "status" on one view. Because both lists are coming from one model, is it necessary to create a viewmodel?

I have tried the following, but my view is not finding the item type for each list.

 public class PipelineViewModel
 {
    public int LeadID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Status{ get; set; }
    public string LoanAgent{ get; set; }

    public List<WebLead> PipeNewLeads { get; set; }
    public List<WebLead> PipeDispLeads { get; set; }
}

Note the List is domain model for the table I am pulling the lists from. Is that correct?

Next in my controller:

    public ActionResult Index(string LoanAgent)
    {
        var viewModel = new PipelineViewModel
        {
            PipeNewLeads = db.WebLeads
            .Where(l => l.LoanAgent.Equals(LoanAgent) && l.Status.Equals("New")).ToList(),
             PipeDispLeads = db.WebLeads
            .Where(l => l.LoanAgent.Equals(LoanAgent) && l.Status.Equals("Disp")).ToList(),
         };
        return View(viewModel);

I know that the controller is wrong. I need to be referencing the viewmodel somehow, but I have tried a few ways and keep getting errors.

In the view, I used

@model LoanModule.ViewModels.PipelineViewModel

and I tried to call each list like this, but it didn't work.

@foreach (var item in Model.PipeNewLead)
@foreach (var item in Model.DispNewLead)

I think I am almost there, but I am not sure what I am doing wrong in the controller. I would appreciate any help or references!

Upvotes: 0

Views: 7614

Answers (3)

Charles Owen
Charles Owen

Reputation: 2840

You can test to see if you have data from your model in this way:

@if(Model.PipeNewLeads != null && Model.PipeNewLeads.Count > 0)
{
      foreach (var item in Model.PipeNewLeads)
      {

      }          
      else
      {
          <p>No PipeNewLeads</p>
      }
 }

But you really should be creating Unit Tests where you'd be able to determine which models are actually in your view.

Upvotes: 0

PhoenixPan
PhoenixPan

Reputation: 551

Came across this issue...I think it's just a typo.

You have public List<WebLead> PipeNewLeads { get; set; } in model but @foreach (var item in Model.PipeNewLead) in the view. Note the plural, so of course you saw the exceptions your got (not found/does not contain).

Upvotes: 0

Michal Ciechan
Michal Ciechan

Reputation: 13888

Consdering you have:

public ActionResult Index(string LoanAgent)
{
    var viewModel = new PipelineViewModel
    {
        PipeNewLeads = ....
        PipeDispLeads = ....
     };
    return View(viewModel);
}

Your view foreachs should be:

@foreach (var item in Model.PipeNewLeads)
@foreach (var item in Model.PipeDispLeads)

Note the spelling ;-)

This works perfectly fine for me:

    public ActionResult Contact()
    {
        var viewModel = new PipelineViewModel
        {
            PipeNewLeads = new List<WebLead>(),
            PipeDispLeads = new List<WebLead>(),
        };
        return View(viewModel);
    }

And view:

@model WebApplication1.Controllers.PipelineViewModel
@foreach (var item in Model.PipeDispLeads)
{
    <p>Disp</p>
}
@foreach (var item in Model.PipeNewLeads)
{
    <p>New</p>
}

Your issue is somewhere else.

I would:

  1. Look at where you are referencing IEnumerable<PipelineViewModel> and find out why. This is most likely in a completely different Razor view, therefore making that error unrelated.
  2. Check what your entity framework is returning. The EF error you are getting is probably because either:
    • Database not available
    • EF Model is different to database
    • Login issues
    • Network issues

Obviously your EF error could be something different, but those are some suggestions

Upvotes: 1

Related Questions