2

I have these models:

public class Condominium
{
    public int CondominiumID { get; set; }

    public int BatchID { get; set; }

    public string Name { get; set; }

    public bool LiveInCondominium { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }

    public Batch Batch { get; set; }

    public List<Employee> Employees { get; set; }
}

public class Employee
{
    public int EmployeeID { get; set; }

    public int CityID { get; set; }

    public string UserID { get; set; }

    public Condominium CondominiumID { get; set; }

    public string Name { get; set; }

    public string Address { get; set; }

    public string ZipCode { get; set; }

    public string Contact { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }

    public City City { get; set; }

    public Condominium Condominium { get; set; }
}

I need to create Employee objects dynamically and put them into a list and when I make a post request the object Condominium contains a list with Employee objects. I don't have any idea how I can make the view for this.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • do you want to be able to edit any of the employee fields? – user5103147 Jul 16 '15 at 01:24
  • Yes, but more specific I need to create objects of type Employee in the view then put them into a list then set it in Condominium.Employees – Angel Buzany Jul 16 '15 at 16:06
  • 1
    The answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) give some options for dynamically creating collection items –  Jul 20 '15 at 01:58

2 Answers2

2

I suggest that you build View models for each view, in this case you would build a model that contains a property that holds a list of employees. You would then simple fill the model and return it to the view.

Here is some pseudo code:

Controller

public ActionResult Index() 
{
    var viewModel = new ViewModel()
    {
        Employees = BuildListOfEmployees() // Method that gets/builds a list of employees, expected return type List<Employee>
    };

    return View(viewModel);
}

class ViewModel
{
    public List<Employee> Employees { get; set; }
}

View

@model YourNamespace.ViewModel

<ul>
@foreach (var employee in Model)
{
    <li>employee.Name</li>
}
</ul>
Martin
  • 1,634
  • 1
  • 11
  • 24
-1

Typically this information is stored in a database and you just pass the IDs as URL parameters. The HTTP request handler will take the parameter(s) and look up the information it needs from the database.

Your object structure looks pretty flat so they would translate easily to Tables in a relational database.

Steve
  • 623
  • 4
  • 11