Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
I was trying to display the result without model(CLASS),i already have a department and Employee class ,now to show their joined result I don,t want to make another class which contains EmpName and Department Name property



IN MODEL
public class EmpDetails
    {     
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
         public int DepartmentID { get; set; }
}
 public class Department
{
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
        public string Location { get; set; }
}

In Controller
 var q = (from ED in cc.EmpDetails
join DT in cc.Department
on ED.DepartmentID equals DT.DepartmentID
select new {
ED.Name,
DT.DepartmentName
});

ViewBag.ListData = q;

In View
foreach (var item in ViewBag.ListData)
       {
           <div>
               @item.Name
           </div>
        <div>
               @item.DepartmentName
           </div>
       }

I want it to be done without using model

What I have tried:

I tried by making a new model for empName and department name which was succesfull,
but i want to do it without model class
Posted
Updated 6-Jun-16 0:34am
Comments
Karthik_Mahalingam 6-Jun-16 5:49am    
you need only ED.Name & DT.DepartmentName or other properties also ?
glen205 6-Jun-16 5:54am    
It's only my opinion, but I would keep the class. This goes to separation of your domain model, which may directly reflect the structure of your database, and the "model" part of MVC which is a presentation model. If you accept that an application can have multiple models - maybe one for the DB (domain/entity model), one for MVC (presentation) and any number of other models (business logic etc) it becomes acceptable to take a couple of fields from two entity objects and combine them to form a presentation object, which is the "model" for the page you want to render....

The OO concept of a model can therefore cover your presentation layer with a different set of classes to your persistence layer -

TL:DR - it's okay if you have a custom model to present to a view....

Just my 2 cents!
Avnish Maurya 6-Jun-16 5:58am    
other properties too
Karthik_Mahalingam 6-Jun-16 6:27am    
Always use  Reply  button, to post Comments/query to the user, else the User wont get notified.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900