I have a quite simple model where someone can select a responsible employee for an order:

public class Employee
{
  public int Id { get; set; }
  public string Name { get; set; }
}

public class Order
{
  public int ResponsibleEmployeeId { get; set;}
  public IEnumerable<Employee> AllEmployees { get; set; }
  public string Description { get; set; }
}

public class OrderManager
{
  public IEnumerable<Order> AllOrders { get; set; }
}

As you can see, the Order class has a list of all employees to be used in a drop down in the frontend.

These are the Templates: OrderManager.cshtml:

<table>
  <thead>
    <tr>
      <th>Employee</th>
      <th>Desciption</th>
    </tr>
  </thead>
  <tbody>
    @Html.EditorFor(model => model.Allorders)
  </tbody>
</table>

Order.cshtml:

<tr>
  <td>@Html.EditorFor(model => model.Description)</td>
  <td>@Html.DropDownListFor(model => model.ResponsibleEmployeeId, new SelectList(Model.AllEmployees, "Id", "Name", Model.ResponsibleEmployeeId))</td>
</tr>

This way the templates are nicely seperated and Order.cshtml can be used on its own from somewhere else. However each order needs to store all employees, which is not really needed as all orders have the same employees in the drop down. Another way would be to move the AllEmployees list to the AllOrders class. This would cut the memory Footprint (n*m -> n). But this way I would have to store the AllEmployees list in the ViewBag in OrderManager.cshtml to retrieve it in the Order.cshtml.

So what I'm asking for is a better way to store the employee list only one time without creating a strong coupling between OrderManager.cshtml and Order.cshtml.

Related posts

Recent Viewed