razor - ASP.NET MVC 3: Using Enumerable extension methods in the view -
given following razor partial view , understanding product nhibernate mapped object calls ienumerable here fire database queries (when not cached).
is bad practice? should providing flatter view of data view can make these calls in controller/business logic?
@model ienumerable<myproject.data.models.product> <table> <tr> <th></th> <th>total orders</th> <th>fulfilled</th> <th>returned</th> <th>in stock</th> </tr> @foreach (var product in model) { <tr> <td> @html.actionlink(product .name, "detail", "product", new { id = product.id }, null) </td> <td> @product.orders.count </td> <td> @product.orders.where(x=>x.fulfilled).count() </td> <td> @product.orders.where(x=>x.returned).count() </td> <td> @(product.stock.count - product.orders.count) </td> </tr> } </table>
is bad practice?
yes.. in fact it's breaking mvc pattern - view's should not call through model, receive in order it's sole job: rendering html.
if need additional information 1 entity, populate viewmodel information need, pass view.
also, don't loop through ienumerable
in model, use display template:
@html.displayformodel()
the advantage of no explicit loop, taking advantage of mvc conventions, , adhering model hierachy when model binding.
Comments
Post a Comment