Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Pass Multiple Models to View From Controller

I need to have a view that displays Employee First and Last Name and the Employees associated Supervisor First and Last name.

I have 2 models they are as follow:

public class Employee
{
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Department { get; set; }
    public int SupervisorID { get; set; }

    public virtual ICollection<Supervisor> Supervisor { get; set; }
}

public class Supervisor
{
    public int SupervisorID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Department { get; set; }

    public virtual Employee Employee { get; set; }
}

To display the needed data I have created another model:

public class EmployeeSupervisor
{
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Department { get; set; }

    public string SupFirstName { get; set; }
    public string SupLastName { get; set; }
    public string SupPhone { get; set; }
    public string SuoEmail { get; set; }
    public string SupDepartment { get; set; }
}

and in the details action of a controller I do the following:

Employee employee = db.Employees.Find(id);
Supervisor supervisor = db.Supervisors.Find(employee.SupervisorID);

EmployeeSupervisor es = new EmployeeSupervisor
{
    EmployeeID = employee.EmployeeID,
    Department = employee.Department,
    FirstName = employee.FirstName,
    LastName = employee.LastName,
    SupFirstName = supervisor.FirstName,
    SupLastName = supervisor.LastName,
    SuoEmail = supervisor.Email,
    SupPhone = supervisor.Phone,
    SupDepartment = supervisor.Department
};

return View(es);

Then in the view I have

@model TimeOffV3.Models.EmployeeSupervisor

and then lastly I do the following in the view

@Html.DisplayFor(model => model.FirstName)
@Html.DisplayFor(model => model.LastName)
@Html.DisplayFor(model => model.SupFirstName)
@Html.DisplayFor(model => model.SupLastName)

When I accomplish this task as described above with the EmployeeSupervisor model class, entity framework creates a corresponding table for EmployeeSupervisor in the database (as expected). This leads me to believe I'm doing it the wrong way as I can't imagine everytime I want to display data from 2 different models I need to create a new corresponding model and table.

Is the way I accomplished this correct? Should I be able to access the supervisor information using the navigation property defined in the Employee class? Can I pass in multiple models so I don't have to create a model containing the info from 2 separate models?

like image 875
mdrap Avatar asked Nov 29 '25 12:11

mdrap


1 Answers

Indeed view receive only one model but this model doesn't have to be flat. Your view model could contain separate properties for each one of the entities

public class EmployeeSupervisor
{
    public Employee Employee { get; set; }
    public Supervisor Supervisor{ get; set; }
}

And in your view you work with them as following:

   @Html.DisplayFor(model => model.Employee.FirstName)
   @Html.DisplayFor(model => model.Employee.LastName)
   @Html.DisplayFor(model => model.Supervisor.FirstName)
   @Html.DisplayFor(model => model.Supervisor.LastName)

Additional thing that you should know, is that Views don't have to work with entities (those that have corresponding tables in the database). You can create a ViewModels that will contain only the properties that are used in your view. There are some libraries that can help you with mapping between ViewModels and entities, for example Automapper or ValueInjecter

If for example you need to display in your view only full names of employee and supervisor your view model could look like:

public class EmployeeSupervisorViewModel
{
    public string EmployeeName { get; set; }
    public string SupervisorName { get; set; }
}

And the controller:

    Employee employee = db.Employees.Find(id);
    Supervisor supervisor = db.Supervisors.Find(employee.SupervisorID);

    var model = new EmployeeSupervisorViewModel
    {
        EmployeeName =  string.Format("{0} {1}",employee.FirstName, employee.LastName),
        SupervisorName =  string.Format("{0} {1}",supervisor.FirstName, supervisor.LastName),
    };

    return View(model);

EmployeeSupervisorViewModel is not an entity and should not have a database table It is used only on View/Controller level and contains only the information that is required by View (info that you want to render to the client)

like image 92
Alex Art. Avatar answered Dec 02 '25 00:12

Alex Art.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!