You can use data transfer objects for this problem, or you can use the expand methods for the Entity include method in EF -
Nothing is different from primitive properties. If this is not what you mean, correct me, I will help you again.
thanks
DTO:
public class OrderDTO { public int Id { get; set; } public int CustomerId { get; set; } public string CustomerName { get; set; } }
NHibernate Storage:
public IList<OrderDTO> GetOrders() { return Session.Linq<Order>() .Select(o => new OrderDTO { Id = o.Id CustomerId = o.Customer.Id CustomerName = o.Customer.Name ... }).ToList(); }
View:
With Expand - Enable - Model Type - "Order":
<%= Model.Customer.Name %>
With DTO - model type - "OrderDTO":
<%= Model.CustomerName %>
edit:
Ok, first you can use FormViewModel to create / edit actions. eg:
Controller:
public ActionResult Edit(int id) { Order order = null; // OrderService.Get(id); IList<Customer> customers = null; // CustomerService.GetAll(); OrderFormViewModel model = OrderFormViewModel.Create(order); model.Customers = customers.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }); return View(model); } [HttpPost] public ActionResult Edit(int customerId, Order order) { //customerId - selected from dropdown. } public class OrderFormViewModel { public static OrderFormViewModel Create(Order order) { return new OrderFormViewModel { Order = order }; } public Order Order { get; internal set; } public IEnumerable<SelectListItem> Customers { get; internal set; } public int CustomerId { get; internal set; } }
View:
<%= Html.DropDownListFor(o => o.CustomerId, Model.Customers) %>
source share