-1

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.

coder231
  • 453
  • 2
  • 14
  • 1
    Refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options to dynamically add (and delete) child objects –  Jan 08 '16 at 20:59
  • @StephenMuecke - Thank you. These posts help a lot. I couldn't find them when I was searching :) – coder231 Jan 08 '16 at 22:22

1 Answers1

0

create ViewModel like this :

 public class DepartmentViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<EmployeeViewModel> Employees { get; set; }
}
public class EmployeeViewModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}

and fill it from db like this :

 var dept=db.Department.SingleOrDefault(id); // you get id in action param
        var model = new DepartmentViewModel
        {
            Name = dept.Name,
            Id = dept.Id,
            Employees = dept.Employees.Select(row => new EmployeeViewModel
            {
                Age = row.Age,
                Email = row.Email,
                Name = row.Name
            }).toList()
        };

and pass model to View Like this:

return View(model);

and set model for your View Like this :

@model yourNamespace.DepartmentViewModel

and if you need to update DepartMent with your ViewModel , you can do like this :

  public ActionResult Update(DepartmentViewModel deptViewModel)
    {
        var dept = db.Department.SingleOrDefault(deptViewModel.Id);
        dept.Name = deptViewModel.Name;
           dept.Employees = deptViewModel.Employees.Select(row => new Employee { 
        Name=row.Name
        }).Tolist();
    }
Uthman Rahimi
  • 708
  • 5
  • 22
  • I understand that this approach can be used when you want to populate value from database. But my question is for a 'create' view. I need to get the values from user and then update the database. – coder231 Jan 08 '16 at 20:58
  • So what's the problem in using "empty" model object and then store it? – Andrew Jan 08 '16 at 21:02
  • @Andrew - I am not able to access the list class element names (Employee) in my view when passing empty model object. eg.model=>model.Employees.Name is not accessible. I can only access items in my first class. – coder231 Jan 08 '16 at 21:12
  • @UthmanRahimi - I am looking for a fresh 'create' view. I need not pass any value from database to my view. All values are to be got from the user and updated. So, If I create a viewmodel as suggessted by you, I am not able to access any elements from EmployeeViewModel in my view. – coder231 Jan 08 '16 at 21:14
  • But you can do it all with JavaScript. Moreover, you can add button for "Add item" action (and I guess you have it) and loop through the objects in your view with c# code – Andrew Jan 08 '16 at 21:15
  • @Andrew - Thank you.. I will figure it out. Can I still access the list values in my controller without any trouble if I use javascript? – coder231 Jan 08 '16 at 21:25
  • @Bharathi yes, if you create json object with correct structure and then model binder can bind your json to your model object – Andrew Jan 08 '16 at 21:28