Hi everyone,
I would like to ask how can I display the service ratings in view.
For now, I have put in one SQL Statement in my controller:
- SELECT s.Services_id, s.Service_Name,s.Service_Description,s.Price,AVG(r.Score)
- FROM Rating r
- FULL OUTER JOIN Services s ON r.Services_Id = s.Services_Id
- WHERE s.Service_Status = 'active'
- GROUP BY s.Services_id,r.Services_Id,s.Service_Name,s.Service_Description,s.Price
In my model:
- public class Services
- {
- public int Services_id {get; set;}
- public string Service_Name { get; set; }
- public string Service_Description { get; set; }
- public Decimal Price { get; set; }
- public string Service_Status { get; set; }
- public int Service_Provider_Id { get; set; }
- public int Currency_Id { get; set; }
- public string Currency_Name { get; set; }
- public int Category_id { get; set; }
- public int Rating_Id { get; set; }
- public int Score { get; set; }
- public int Service_Score { get; set; }
- }
In my view:
- @{
- Layout = "_Guest_NavBar";
- }
- @model List
ServeTheWorld - "~/lib/datatables/css/jquery.dataTables.min.css" rel="stylesheet" />
Services Available
"DataTable" class="table-style table">
- @{
- var sno = 1;
- }
No. Service Name Service Description Service Price Ratings Actions - @foreach (Services s in Model)
- {
@s.Services_id @s.Service_Name @s.Service_Description @s.Price - @for (int i = 0; i < (Model.Sum(s=>s.Score)/Model.Count); i++)
- {
- class="fa fa-star" style="color:darkorange">
- }
- @for (int i = (Model.Sum(s => s.Score) / Model.Count); i < 5; i++)
- {
- class="fa fa-star-o" style="color:darkorange">
- }
"Homepage" asp-action="ViewServices" asp-route-id="@s.Services_id">class="fa fa-info-circle" style="font-size:30px"> - }
Now, I want to display all the services and their rating score on the webpage.
When I run once with the SQL Statement that I put into my controller action, it does not show the rating on the webpage.
For example:
In my database, I have record with the Services_Id = 1 and this id has three rating scores(4,2,4) stored inside the rating table:
So I want to use AVG to calculate the average rating score and it has an average score of 3:
When I run and on the webpage does not display 3 stars which is the average score for Services_Id = 3:
How can I achieve this
Please help
Thank you
Sachin SinghPosted Jan 29, 2021, 12:48 PM
Yong KevinPosted Jan 29, 2021, 1:40 PM
Sachin SinghPosted Jan 29, 2021, 1:26 PM
Yong KevinPosted Jan 29, 2021, 12:57 PM