Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC:一个月内每天的销售额_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc ASP.NET MVC:一个月内每天的销售额

Asp.net mvc ASP.NET MVC:一个月内每天的销售额,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我使用的是ASP.NETMVC5EF6。我已应用逻辑生成报告,其中显示所选月份每天的所有销售额。这几天似乎效果不错。但后来我做了一些改变。现在它没有按顺序显示日期。我已经检查过了,找不到bug。请大家检查一下,告诉我哪里出错了 我的控制器 public ActionResult MonthlySalesByDate(string _year, string _month) { //assign incoming values to the variables

我使用的是ASP.NETMVC5EF6。我已应用逻辑生成报告,其中显示所选月份每天的所有销售额。这几天似乎效果不错。但后来我做了一些改变。现在它没有按顺序显示日期。我已经检查过了,找不到bug。请大家检查一下,告诉我哪里出错了

我的控制器

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; }
}
公共类DayTotalVM
{
公共整数日{get;set;}
公共十进制总数{get;set;}
}
公共类SalesVM
{
公共日期时间日期{get;set;}
公共列表天数{get;set;}
}
视图:


白天
数量
@{十进制总数=0;}
@对于(int i=0;im.Days[i].Day);
DateTime dateValue=新的日期时间(年、月、型号.Days[i].Day);
:@dateValue.ToString(“dddd”)
}
@{
if(Convert.ToString(Html.DisplayFor(m=>m.Days[i].Total))!=“0.00”)
{
@Html.DisplayFor(m=>m.Days[i].Total)
}
其他的
{
@Html.DisplayFor(m=>m.Days[i].Total)
}
}
@{total+=(十进制)(Model.Days[i].total);}
}
总数:
@总数
结果:


您没有在任何地方排序结果。您应该这样做(假设您希望它们按升序排列):


您没有在任何地方订购结果。您应该这样做(假设您希望它们按升序排列):


谢谢@ArtOfCode.。我没意识到!谢谢@ArtOfCode.。我没意识到!
 <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>
var query = 
    db.Sales.Where(x => x.Date.Year == year 
                        && x.Date.Month == month)
            .OrderBy(x => x.Date)
            .Select(g => new
            {
                Day = g.Date.Day,
                Total = g.GrandTotal
            });