Show Total order value on View Asp.net mvc

Asked

Viewed 603 times

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 ?

  • 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

  • how is the code in the Controller ?!

  • @ Jcsain put to Voce see, in view details I can see total value, in view index no

  • 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 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

  • @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 the item.RelatorioDetalhes.FirstOrDefault().valor for item.RelatorioDetalhes.Sum(v => v.valor)

  • @ Kelvynrisso Man if I could buy you a case of beer now, thank you very much, it worked exactly as I wanted it

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.