Question

I have an OrderViewModel that includes an instance of a child _DetailsViewModel. The OrderViewModel has order header information and the _DetailsViewModel holds order detail information. Despite being separate Models, both have the same single data source..the Orders table. The reason that the details are in their own View Model is that those fields are reused on a different View, in the same visual arrangement, so I'm putting them in a Partial View to be reused as needed. Here is an idea of my main and child View Models:

public class OrderViewModel
{
    public string OrderNum  { get; set; }
    public string CustonerName{ get; set; }
    public double SalesPrice{ get; set; }
    public _Details Details { get; set; }
}

public class _DetailsViewModel
{
    public string PhoneNum { get; set; }
    public string ItemNum { get; set; }
    public double Quantity { get; set; }
    public string PayMethod{ get; set; }
    public string Comments { get; set; }
}

Within my controller I call a service that returns all data from the Orders table and returns a List of Order Entities.

orderService = new OrderService();
var orders = orderService.GetOrderInfo(StoreNum);

From there I use Omu.ValueInjecter to inject the results into the main View Model.

var orderViewModel = orders
                    .Select(x => new                              
                      OrderViewModel().InjectFrom(x)).Cast<OrderViewModel>()
                    .ToList();

return View(orderViewModel);

I need to also populate the _Details model so that I can pass it to the Partial View from within my main Order View...like below:

@Html.Partial("_OrderDetails", Model._Details)

Is there a way to populate the _Details Model from the single service call that is already populating the main Order Model? Will I have to add the _Details properties to the main Order View and then iterate the Order view to set each field of the corresponding _Details Model manually? Surely I'm missing something.

Thanks...

Était-ce utile?

La solution

Move the entities out of your database first, in that manner you only issue one query request:

// apply any filter(s) needed here.
var orderList = orders.ToList();

// then do injecting using the "cached" orders 
var orderViewModel = orderList
    .Select(x => new OrderViewModel().InjectFrom(x))
    .Cast<OrderViewModel>()
    .ToList();

// then inject into your details model
var detailsModel = orderList
    .Select(x => new _DetailsViewModel().InjectFrom(x))
    .Cast<_DetailsViewModel>()
    .ToList();

And a small suggestion if I may, remove the underscore for _DetailsViewModel to make the naming standard.

UPDATE:

How do I add the detailsModel to the orderViewModel afterwards to pass to the Order View?

You just simply set it to the instance of OrderViewModel like so:

orderViewModel.Details = detailsModel ;

Then return orderViewModel to your view and do your thing there:

@Html.Partial("_OrderDetails", Model.Details)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top