1
Hello Emmanuel,
You can try the below code. you need to sum up the quantity to get the item quantity count and get the critical stock needed group by stock item and sum up. if the total is less than the critical count consider it as critical stock count.
public JsonResult GetDashboardData()
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var currentDate = DateTime.Now.Date.ToString("dd-MMM-yyyy");
var totalQty = db.tblProductStock.Sum(a = >a.Quantity);
var criticalStockCount = db.tblProductStock.GroupBy(a=>a.productStockId).Where(a
=>a.Sum(b=>b.Quantity) <= 0).Count();
return Json(new{totalQty = totalQty ,criticalStockCount =
criticalStockCount},JsonRequestBehavior.AllowGet);
}
Vishal Joshi
Thanks
Accepted 0
Hi Emmanuel. Typically I would like to say this is aggregated data, so instead of computing from a transaction table. I suggest creating another data product which supposed to act as aggregated data that you are planning to display in the dashboard.
Thanks
Aravind Govindaraj
0
Thank you team,
Everything is working now.
0
try something like this
public class DashboardData
{
public int ItemQuantity { get; set; }
public int CriticalStock { get; set; }
}
public JsonResult GetDashboardData()
{
using (ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities())
{
var currentDate = DateTime.Now.Date;
// Assuming you have a table named "Items" with fields for quantity and critical stock
int totalItems = db.Items.Sum(item => item.Quantity); // Calculate total quantity of items
int criticalItems = db.Items.Count(item => item.Stock < item.CriticalThreshold); // Count items that are critical
DashboardData dashboardData = new DashboardData
{
ItemQuantity = totalItems,
CriticalStock = criticalItems
};
return Json(dashboardData, JsonRequestBehavior.AllowGet);
}
}
Replace the entity names and fields (Items
, Quantity
, Stock
, CriticalThreshold
, etc.) with the appropriate names from your database.
Once you've retrieved the required data (total quantity and critical stock count), you create an instance of a class (DashboardData
) with the retrieved values and return it as JSON in the response.
Make sure to handle errors or exceptions appropriately and ensure that your Entity Framework model matches your database schema for this code to work correctly.
Finally, in your frontend code (JavaScript or whatever you're using), you can fetch this JSON data from your MVC Controller and render it on your dashboard as needed.
