asp.net mvc - Entity Framework use function from other repository -


i have asp.net mvc5 application store. application built, have add features.

the application uses domain layer interfaces , entity framework.

there database table products, , created table reviews. tables linked productid <> reviewid , have one-to-many relationship.

products:

  • productid
  • productname
  • productprice

review:

  • reviewid
  • reviewname
  • reviewrating
  • reviewtext

i have created new entity framework reviewrepository function:

getaverageforproduct(int productid)  

which calculates average review rating.

    public double getreviewaverage(int productid)     {         if (context.review.count(x => x.product.productid == productid) == 0)         {             return 0;         }         else         {             return math.round(context.review.where(x => x.product.productid == productid).average(x => x.reviewrating), 0);         }     } 

now want show average rating on product page.

the view set this:

    @model ienumerable<domain.entities.products> 

the products show following loop:

    @foreach (var m in model)     {     } 

how can calculate average review rating every product? because view uses productrepository controller (function: getallproducts()), not reviewrepository.

the application not use view models.

solved problem. created simple view model.

the viewmodel:

public class popularproductlist {     public product product { get; set; }     public double average { get; set; } } 

in controller:

        var model = new list<popularproductlist>();          foreach (var p in productrepository.getpopularproducts())         {             var productlist = new popularproductlist();             productlist.product = p;             productlist.average = reviewrepository.getreviewaverage(p.productid);              model.add(productlist);         }          return view("index", model); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -