I have a View Index where I will show the name of the store the total order and add all orders and show the total value. I created a ReportAmazing. I can get the total of orders. In my table it is already right with the total value correto, but in the view the total value it only brings the single value of a product.
public class RelatorioArmazem
{
[Key]
public int relatorioId { get; set; }
public int ArmazemId { get; set; }
public decimal valor { get; set; }
public decimal valorTotal { get; set; }
public string Nome { get; set; }
public int totalPedido { get; set; }
[ForeignKey("ArmazemId")]
public virtual Armazem Armazem { get; set; }
}
Warehouse
public class Armazem
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Nome { get; set; }
public int Prazo { get; set; }
public virtual ICollection<Deposito> Deposito { get; set; }
public virtual ICollection<RelatorioArmazem> RelatorioDetalhes { get; set; }
}
My View
@model IEnumerable<ABC.Models.Data.Armazem>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Nome)
</th>
<th>
Total Pedido
</th>
<th>
R$ Total
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatorioDetalhes.Count)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatorioDetalhes.FirstOrDefault().valor)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
public ActionResult Index()
{
return View(db.Armazem.ToList());
}
public ActionResult Details(int id)
{
var ra = db.RelatorioArmazemDetalhes.Where(x => x.ArmazemId == id).ToList() ;
var total = ra.Sum(x => x.valor);
RelatorioArmazem arm = new RelatorioArmazem();
foreach (var item in ra)
{
arm.totalPedido = ra.Count;
arm.valor = item.valor;
arm.valorTotal = total;
arm.Nome = item.Nome;
ViewBag.ValorTotal = total;
}
return View(arm);
}