asp.net mvc - How to pass collection of anonymous objects from controller to view -


I'm new to MVC, so please forgive me if my question seems silly or very simple. I am using an entity data model for So in my model folder, I've added an EDMX file and I can use model classes and strongly typed ideas from my controllers. The problem arises when I access more than one table in my controller like

If I have the following tables in my DB:

  Departments (DepartmentID, DepartmentName, DepartmentPhone) Insurance (InsuranceID, InsuranceName, InsuranceAddress) Employee (EmployeeID, EmpFName, EmpLName, DepartmentID, InsuranceID)   

And I want to display a list of employees with my department and insurance information.

In my controller action method I can access DB using EDM and get information in an anonymous type:

 using  (above reference = new MyEntities ( )) {Var model = (reference.Departments in D.DepartmentID on d.DepartmentID is equivalent to joining e in context.Employees e.DepartmentID I.InsuranceID on context.Insurances to join e.InsuranceID new selection equal {DepartmentID = d.DepartmentID, EmployeeID = e.EmployeeID., EmpFName = e.EmpFName, EmpLName = e.EmpLName, DepartmentName = d.DepartmentName, InsuranceName = I.InsuranceName}) ToList (); See Return (Model); }   

I do not have a class of this anonymous type in my model folder, so if I can not make a properly typed view, what is the best way to pass this list to see is? If the scene is huge then using the viewbag will be an overkill, it does not look right to create a new model for this anonymous class because if I change my selection in the Controllers Action Method, then it needs to be updated at all times.

All suggestions are welcome. I tried to look at the SO through other questions but nothing was found relevant.

Thank you.

I can not create a strongly typed view

Right click on your project, my class folder does not need a square of this anonymous type ... and now you have a type in your model folder. This ASP.Net MVC = & gt; There is a way to go in. See the model.

And then obviously you pass in your view like this:

  select new MyViewModel (Department ID = D. DepartmentID, EmployeeID = e.EmployeeID, EmpFName = E .EmpFName, EmpLName = e.EmpLName, partition name = d.DepartmentName, InsuranceName = I.InsuranceName}). ToList ();   

And of course, your view is now strongly typed on this visual model:

  @modelIINMereble & lt; MyViewModel & gt; ...    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -