public class ImageVm
{
public string ImageUrl { get; set; }
public Line[] lines { get; set; }
}
public class Line
{
public int number { get; set; }
public Product[] products { get; set; }
public string products_position_average { get; set; }
public string line_average { get; set; }
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public int count { get; set; }
public int master_count { get; set; }
public string count_average { get; set; }
public string position_average { get; set; }
}
Keep your friends close and your enemies even closer
Can't I use aggregate functions on View via model? Because model is already on the view and it has all the data. Besides I have to calculate 2 more sums. If your way is the only one, how can I send those all sums to the View in the controller?
[HttpGet]
public ActionResult FakeAnalyze(string image)
{
ImageVm vm = new ImageVm();
//string img = string.Empty;
using (StreamReader file = System.IO.File.OpenText(@"C:\PlanogramWebApp\PlanogramWebApp\images\test.json"))
{
JsonSerializer serializer = new JsonSerializer();
vm = (ImageVm)serializer.Deserialize(file, typeof(ImageVm));
}
// Convert byte[] to Base64 String
string base64String = vm.ImageUrl;
vm.ImageUrl = "data:image/png; base64," + base64String;
return View("Index", vm);
}
edit: I got what you mean :)
@Model.lines[i].products.Sum(m => m.master_count)
Best Regards.
Keep your friends close and your enemies even closer
Member
526 Points
2722 Posts
Razor how to sum model data
Jan 09, 2019 10:51 AM|cenk1536|LINK
Hello guys,
I need to get sum of model data but couldn't manage how to implement it. Any help would be great?
Thanks in advance.
Here is my model:
All-Star
52151 Points
23250 Posts
Re: Razor how to sum model data
Jan 09, 2019 02:52 PM|mgebhard|LINK
The linq syntax is...
int masterCountTotalPerProduct = 0; using (StreamReader file = File.OpenText("data.json")) { JsonSerializer serializer = new JsonSerializer(); JsonResponse response = (JsonResponse)serializer.Deserialize(file, typeof(JsonResponse)); masterCountTotalPerProduct = response.lines[0].products.Sum(m => m.master_count); }
Linq reference docs.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-linq
Member
526 Points
2722 Posts
Re: Razor how to sum model data
Jan 09, 2019 06:08 PM|cenk1536|LINK
Hi mgebhard,
Can't I use aggregate functions on View via model? Because model is already on the view and it has all the data. Besides I have to calculate 2 more sums. If your way is the only one, how can I send those all sums to the View in the controller?
edit: I got what you mean :)
Best Regards.