1

I have two tables whose structure is as follows:

Employee:

int Id
string EmployeeName 
string Department

Department:

int Dept_Id
string Department   

Department Table Stores All the Departments with their respective id, and i have a registration form of Employee which contains DropDownList. I want to populate all of the departments as soon as the form loads in the dropdown for that i have to call two models on the same view. I was using tuple but it does not solve my problem. Please help to solve my problem. Code that i am using is :

public ActionResult Index()
{

 var model = new model<Employee, Department>(new Employee(), new Department());

    return View(model);
}


@model Tuple<MVcEmpApp.Models.Employee, MVcEmpApp.Models.Department>    


[HttpPost]
public ActionResult InsertRecord(MyViewModel model)
{
if(ModelState.IsValid)
 {
   var Employee=new Employee();
   Employee.EmployeeName=model.employee.EmployeeName;
   Employee.Department=model.employee.Department;
   db.AddToEmployee(Employee);
   db.SaveChanges();
  }
}

Please give any suggestions, Thanks for your answers....

user3026519
  • 543
  • 7
  • 17
  • 36

2 Answers2

10

You can create a single ViewModel wrapper class which holds your two models. Read What is ViewModel in MVC?

public class MyViewModel
{
  public Employee employee{get;set;}
  public Department dept{get;set;}

}

Controller

public ActionResult Index()
{

    var model = new MyViewModel{employee=new Employee(), dept=new Department()}

    return View(model);
}

In your view

@model MyViewModel

<label>First Name</label> 
 @Html.TextBoxFor(m=>m.Employee.FirstName)
Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • Thanks Murali... Now it is working ... Can you please tell the correct way to add and edit records when working with two models as the methods told by you. I have edited my code and written insert method there. Normally Insert and Edit code i knew.... – user3026519 Jan 10 '14 at 16:55
  • 1
    @user3026519, This is most widely used approach in MVC. The view model is tend to interact with View. The view may not represent all the properties of model, example CreatedDateTime & CreatedBy can be detected based on context user logged in. Usually when you click a record to edit, you need to load the data first and allow the user. To load data, especially in ajax world people are interested in ajaxify way, for user experience. For this it is better load the context data using ajax and replace in right side. The right side view is interested to load the data for the selected id. So I did it – Murali Murugesan Jan 10 '14 at 22:13
0

I would create two classes Employee and Department as entities.

Your model then would be

public class MyModel 
{
    public Employee employee {get; set;}

    public List<Department> departments {get; set;}
}

You can in this scenario wire up your dropdown to the departments. You will want to add javascript to populate a hidden field on your page with the selected value - which you can wireup to the employee's department id field. This way when you submit your form your action can be written as -

public ActionResult Submit(Employee employee) { .... }
Gjohn
  • 1,261
  • 1
  • 8
  • 12