1

How to pass multiple objects to view without using ViewData and ViewBag?

Example:

[AllowAnonymous]
public ActionResult RandomView()
{
   // Customer.
   var customers = _context.Customers.ToList();
   List<CustomerDto> customersDto = new List<CustomerDto>();

   foreach (var customer in customers)
   {
      customersDto.Add(new CustomerDto() { 
      Id = customer.Id,
      Name = customer.Name
      });
   }

   // Reports.
   var Reports= _context.Reports.ToList();
   List<ReportsDto> reportsDto= new List<ReportsDto>();

   foreach (var report in reports)
   {
      reportsDto.Add(new ReportsDto() {
      Id = report.Id,
      Name = report.Name
      });
   }

   return View(); // How to Pass Both CustomerDto and reportDto ?
}

I want to pass both CustomerDto & reportDto as a strongly type to a view. Is it Possible ?

Rehan Shah
  • 1,505
  • 12
  • 28

3 Answers3

3

Provide a model class for your view. In that view, you reference your model class. Make the two lists properties of your model class. See sample below:

public class RandomViewModel
{
    public List<CustomerDto> Customers { get; set; }
    public List<ReportDto> Reports { get; set; }
}

[AllowAnonymous]
public ActionResult RandomView()
{
   // Customer.
   var customers = _context.Customers.ToList();
   List<CustomerDto> customersDto = new List<CustomerDto>();

   foreach (var customer in customers)
   {
      customersDto.Add(new CustomerDto() { 
      Id = customer.Id,
      Name = customer.Name
      });
   }

   // Reports.
   var Reports= _context.Reports.ToList();
   List<ReportsDto> reportsDto= new List<ReportsDto>();

   foreach (var report in reports)
   {
      reportsDto.Add(new ReportsDto() {
      Id = report.Id,
      Name = report.Name
      });
   }

   var randomViewModel = new RandomViewModel() {
      Customers = customersDto,
      Reports = reportsDto
   };

   return View(randomViewModel);
}
2

You should wrap both types to some ViewModel class something like below:

public class CustomerData
{
    public List<CustomerDto> Customer { get; set; }
    public List<ReportsDto> Reports { get; set; }
}

and then pass to your view:

var customerData = new CustomerData() 
{
   Customer = customersDto,
   Reports = reportsDto
}
return View(customerData);
Ambroży
  • 75
  • 5
  • Why downvote this? That's how results are returned in MVC - create a ViewModel with all required data. It's the same as the other answers too – Panagiotis Kanavos Jan 21 '19 at 08:24
2

Way 1:

Create a view model that has both properties & pass that view-model to view

public class ViewModel
{
   public List<CustomerDto> Customers {get;set;}
   public List<ReportsDto> Reports {get;set;}
}

Way 2:

Use Tuple & pass that Tuple object to view

var tupleModel = new Tuple<List<CustomerDto>, List<ReportsDto>>(GetCustomers(), GetReports());  
return View(tupleModel);  

In view

Make the view strongly typed with Tuple

@model Tuple <List<CustomerDto>, List <ReportsDto>> 

Access the both objects using Item1 & Item2 properties

@Model.Item1
@Model.Item2
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57