Say I have a model like below. One class has a reference to a collection of another class objects.
public class Department
{
public int Id{ get; set; }
public string Name{ get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
How can I accomplish a 'create' view for both these classes(in a single view) and successfully access the values from view in my controller post action ?(i.e I want to get the values for these fields from user and update the database). If I use viewmodel, how can i access the collection in view and the values from view in controller?
I understand from http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ post on how to handle collection of data from view. But my problem is handling 2 models in a single view, one of which is a collection.
Controller action -
public ActionResult Create()
{
return View();
}
I am not able to access any elements from Employee model in my view. How can I modify the action method above to be able to access Employee model in my view?
I even tried using tuple to merge both models, but that doesn't work either. Again, I am not able to access the Employee model in my view.