0

i can send one database fetch result as a list from controller to view by using this method:

var all_companies = db_cnx.Db.tbl_company.ToList();
var all_contractors = db_cnx.Db.tbl_contractor.toList();

return View(all_companies);

but how can i send more than one objects or list , ... to view? like:

return view(all_companies, all_contractors)

is this possible? i read a comment that said you must declare one ViewItem and store the combination of vars in it and return new data set but how?

Farsheed Feeruzy
  • 175
  • 1
  • 10

1 Answers1

2

To View you can return only one class.

You have to create a model class with those lists and pass that model.

public class MyModel {
   public List<Company> Companies {get;set;}
   public List<Contractor> Contractors {get;set; }
}

var all_companies = db_cnx.Db.tbl_company.ToList();
var all_contractors = db_cnx.Db.tbl_contractor.toList();
var model = new MyModel {
    Companies = all_companies,
    Contractos = all_contractors
}

return View(model);

In View you are using your model instead of lists like that:

@model MyModel

and usage:

@Model.Companies 
@Model.Contractors
madoxdev
  • 3,770
  • 1
  • 24
  • 39