Add columns of a mvc table

0

I have a table created in this way

<table class="table table-bordered table-striped">
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.Sale.Date)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Sale.Warehouse.Name)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Description)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Price)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Quantity)
                        </th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Sale.Date)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Sale.Warehouse.Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Description)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Price)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Quantity)
                            </td>
                        </tr>
                    }
                    <tr>
                        <td colspan="3" align="center">
                            <strong>Totals</strong>
                        </td>
                        <td align="center">
                            <strong>Total Price</strong>
                        </td>
                        <td align="center">
                            <strong>Total Quantity</strong>
                        </td>
                    </tr>
                </table>

I would need to add the last two columns and add the totals in Total Price and Total Quantity respectively. My question is how to do it using javascript, from the controller or from the model? This is my model:

public class SaleDetail
{
    [Key]
    public int SaleDetailId { get; set; }

    [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorFieldRequired")]
    public int SaleId { get; set; }

    [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorFieldRequired")]
    public int ProductId { get; set; }

    [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorFieldRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorStringMaxMin", MinimumLength = 1)]
    [Display(ResourceType = typeof(Strings), Name = "Product")]
    public string Description { get; set; }

    [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorFieldRequired")]
    [DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = false)]
    [Range(0, double.MaxValue, ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorDecimal")]
    [Display(ResourceType = typeof(Strings), Name = "Price")]
    public decimal Price { get; set; }

    [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorFieldRequired")]
    [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = false)]
    [Range(0, double.MaxValue, ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ErrorDecimal")]
    [Display(ResourceType = typeof(Strings), Name = "Quantity")]
    public double Quantity { get; set; }

    public virtual Sale Sale { get; set; }

    public virtual Product Product { get; set; }
}

And this the controller:

public ActionResult DateSales(string value)
    {
            var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            var view = db.SaleDetails.Include(s => s.Sale).Where(s => s.Sale.Warehouse.CompanyId == user.CompanyId && s.Sale.Date == DateTime.Today);
            return View(view.ToList());
    }

Thank you.

    
asked by Nicolas Comaschi 18.07.2018 в 23:10
source

3 answers

1

Even though Xique's answer is correct in terms of solving the problem, it is a bad practice to put logic into the view. The views, in a MVC model, are only to represent the information they receive and nothing more. This logic should go within the model you have, by way of helpers methods: summarizeTotalDetails(list details) {...}  or, better yet, in a helper class that specializes in this type of calculations ( SaleDetalesSummarizer , for example). With which your code is reusable by other controllers:)

greetings

    
answered by 18.07.2018 / 23:57
source
1

In the view it would be declaring two variables outside of your foreach and already within you adding the amounts more or less like this:

<table class="table table-bordered table-striped">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Sale.Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sale.Warehouse.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Quantity)
        </th>
    </tr>
        var PriceTot=0.0M;
        var QuantityTot =0.0D;
    @foreach (var item in Model)
    {

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Sale.Date)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Sale.Warehouse.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Quantity)
            </td>
        </tr>
        PriceTot+=item.Price;
        QuantityTot+=item.Quantity;
    }
    <tr>
        <td colspan="3" align="center">
            <strong>Totals</strong>
        </td>
        <td align="center">
            <strong>Total Price :@PriceTot</strong>
        </td>
        <td align="center">
            <strong>Total Quantity: @QuantityTot</strong>
        </td>
    </tr>
</table>

Another option would be to add them to your controller and just pass the data to your view with something like this:

public ActionResult DateSales(string value)
    {
            var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            var view = db.SaleDetails.Include(s => s.Sale).Where(s => s.Sale.Warehouse.CompanyId == user.CompanyId && s.Sale.Date == DateTime.Today);
            return View(view.ToList());
            ViewBag.TotalPrice=view.Sum(m=>m.Price);
            ViewBag.TotalQuantity=view.Sum(m=>m.Quantity);
    }

and already in the view you only put your ViewBag in the table

   <tr>
        <td colspan="3" align="center">
            <strong>Totals</strong>
        </td>
        <td align="center">
            <strong>Total Price :@ViewBag.TotalPrice</strong>
        </td>
        <td align="center">
            <strong>Total Quantity: @ViewBag.TotalQuantity</strong>
        </td>
    </tr>
    
answered by 18.07.2018 в 23:42
0

I decided to do it from the model. What I did was create a class that inherits the original model and do the addition already, something like this:

[NotMapped]
public class DateSaleView : SaleDetail
{
    public List<SaleDetail> Details { get; set; }

    [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = false)]
    public double TotalQuantity { get { return Details == null ? 0 : Details.Sum(d => d.Quantity); } }

    [DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = false)]
    public decimal TotalValue { get { return Details == null ? 0 : Details.Sum(d => d.Price); } }
}
    
answered by 19.07.2018 в 21:59