ASP.NET MVC:每月销售一个月

时间:2016-01-19 16:29:02

标签: asp.net-mvc entity-framework

我正在使用ASP.NET MVC 5 EF 6.我已应用逻辑生成报告,该报告显示每天所选月份的所有销售额。几天似乎工作正常。但后来我做了一些改变。现在它没有按顺序显示日期。我检查过,找不到错误。请大家检查一下,让我知道我在哪里弄错了?。

我的控制器

public ActionResult MonthlySalesByDate(string _year, string _month)
    {
        //assign incoming values to the variables
        int year =0 , month =0 ;
        //check if year is null
        if ( string.IsNullOrWhiteSpace(_year)  && _month != null)
        {
            year = DateTime.Now.Date.Year;
            month = Convert.ToInt32(_month.Trim());
        }
        else
        {
            year = Convert.ToInt32(_year.Trim());
            month = Convert.ToInt32(_month.Trim());
        }
        //calculate ttal number of days in a particular month for a that year 
        int daysInMonth = DateTime.DaysInMonth(year, month);
        var days = Enumerable.Range(1, daysInMonth);
        var query = db.Sales.Where(x => x.Date.Year == year && x.Date.Month == month).Select(g => new
        {
            Day = g.Date.Day,
            Total = g.GrandTotal
        });
        var model = new SalesVM
        {
            Date = new DateTime(year, month, 1),
            Days = days.GroupJoin(query, d => d, q => q.Day, (d, q) => new DayTotalVM
            {
                Day = d,
                Total = q.Sum(x => x.Total)
            }).ToList()
        };
        return View(model);
    }

我的视图模型:

public class DayTotalVM
{
    public int Day { get; set; }
    public decimal Total { get; set; }
}
public class SalesVM
{
    public DateTime Date { get; set; }
    public List<DayTotalVM> Days { get; set; }
}

查看:

 <table class="table table-striped" id="datatable">
    <thead>
        <tr class="dataTableHead">
            <th>Day</th>
            <th>Amount</th>
        </tr>
    </thead>
    @{decimal total = 0;}
    @for (int i = 0; i < Model.Days.Count; i++)
    {
        <tr>
            <td>
                @{
                    <text>@year - @mnth - </text>
                    @Html.DisplayFor(m => m.Days[i].Day);
                                                        DateTime dateValue = new DateTime(year, mnth, Model.Days[i].Day);
                                                        <text> :  @dateValue.ToString("dddd")</text>

                }
            </td>
            <td class="sales">

                @{

        if (Convert.ToString(Html.DisplayFor(m => m.Days[i].Total)) != "0.00")
        {
            <strong style="color:#4800ff;"><strong> @Html.DisplayFor(m => m.Days[i].Total) </strong></strong>
        }
        else
        {
            @Html.DisplayFor(m => m.Days[i].Total)
        }
                }
                @{total += (decimal)(Model.Days[i].Total);}
            </td>

        </tr>
    }
    <tfoot class="dataTableHead">
        <tr>
            <td>
                <h4 class="pull-right">Total :</h4>
            </td>
            <td>
                <h4>@total</h4>
            </td>
        </tr>
    </tfoot>
</table>

结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

您没有在任何地方订购结果。你应该做这样的事情(假设你想要它们按升序排列):

{{1}}