c# - ASP.NET MVC Pass Multiple Models to View From Controller -


i need have view displays employee first , last name , employees associated supervisor first , last name.

i have 2 models 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 needed data have created 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 details action of controller 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 view have

@model timeoffv3.models.employeesupervisor 

and lastly following in view

@html.displayfor(model => model.firstname) @html.displayfor(model => model.lastname) @html.displayfor(model => model.supfirstname) @html.displayfor(model => model.suplastname) 

when accomplish task described above employeesupervisor model class, entity framework creates corresponding table employeesupervisor in database (as expected). leads me believe i'm doing wrong way can't imagine everytime want display data 2 different models need create new corresponding model , table.

is way accomplished correct? should able access supervisor information using navigation property defined in employee class? can pass in multiple models don't have create model containing info 2 separate models?

indeed view receive 1 model model doesn't have flat. view model contain separate properties each 1 of entities

public class employeesupervisor {     public employee employee { get; set; }     public supervisor supervisor{ get; set; } } 

and in view work them 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 should know, views don't have work entities (those have corresponding tables in database). can create viewmodels contain properties used in view. there libraries can mapping between viewmodels , entities, example automapper or valueinjecter

if example need display in view full names of employee , supervisor view model like:

public class employeesupervisorviewmodel {     public string employeename { get; set; }     public string supervisorname { get; set; } } 

and 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 not entity , should not have database table used on view/controller level , contains information required view (info want render client)


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -