0
I have a View Index where I will show the name of the store the order total and sum all orders and show the total value. I created a Reportstore. I can get total orders. In my table it is already right with the total value corrreto, 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; }
}
Storehouse
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);
}
It would not only change item.RelatoryDetails.Firstordefault(). value per item.RelatoryDetails.Firstordefault(). totalPedido ?
– JcSaint
this only takes total orders, the value variableTotal receives the sum of all orders that I can show in the detail view, but in the index view no
– Cesar Augusto
how is the code in the Controller ?!
– JcSaint
@ Jcsain put to Voce see, in view details I can see total value, in view index no
– Cesar Augusto
Take this chunk of Viewbag code.Total value = total; from within the loop, use a variable to accumulate the total value and then out of the loop, use Viewbag and in View retrieve it using @Viewbag.Total value
– JcSaint
@ Jcsaint this view you’re talking about is of the details la está tudo certo I’ve removed this viewbag from there, the problem in Controller Index, I can’t get the same total value that is in the controlerdetalhes and shows in view Index
– Cesar Augusto
@Cesaraugusto, you can pass the total amount by Viewbag, similar to what you did in Detail, or in the view
Model.Sum(m => m.RelatorioDetalhes.Sum(r => r.valorTotal))
and if the problem is in the view table you posted, you replace theitem.RelatorioDetalhes.FirstOrDefault().valor
foritem.RelatorioDetalhes.Sum(v => v.valor)
– KelvynRisso
@ Kelvynrisso Man if I could buy you a case of beer now, thank you very much, it worked exactly as I wanted it
– Cesar Augusto