繁体   English   中英

显示按年/月/周过滤的数据库中的行 asp.net core mvc by input

[英]Display rows from database filtered by year/month/week asp.net core mvc by input

我目前正在处理销售报告 function。我想通过输入年/月/周来生成销售报告。 在我的 DAL class 中,您可以看到我编写了一个查询,其中显示了 2020 年以来的所有销售额。我在查询中对此进行了硬编码,但我想通过输入显示销售额。 查看下图以获取解释。 用户必须通过输入月份和/或年份来选择显示。 英语不是我的母语抱歉。

这是我的索引视图的图像

这是我的 DAL,出于测试目的,我目前对“01/01/2020”和 OrderDatum <“01/01/2021”进行了硬编码。

public List<OrderModel> getByYear()
    {
        List<OrderModel> Orderlist = new List<OrderModel>();
        try
        {
            string sql = "SELECT * FROM [Order] WHERE OrderDatum >= '01/01/2020' AND OrderDatum < '01/01/2021';";

            DataSet results = ExecuteSql(sql, new List<KeyValuePair<string, string>>());

            for (int x = 0; x < results.Tables[0].Rows.Count; x++)
            {
                OrderModel c = DatasetParser.DataSetToSpeler(results, x);
                Orderlist.Add(c);
            }
            return Orderlist;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

这是我的订单模型

 public class OrderModel
{
    public int BestellingId { get; set; }
    public int KlantID { get;  set; }
    public DateTime Datum { get; set; }
    public double Totaalprijs { get; set; }
    public string Status { get; set; }
    public List<ProductModel> Producten { get; set; }

    public OrderModel(int bestellingId, int klantId, DateTime datum, double totaalPrijs, string status)
    {
        BestellingId = bestellingId;
        KlantID = klantId;
        Datum = datum;
        Totaalprijs = totaalPrijs;
        Status = status;
    }

    public OrderModel()
    {

    }
}

我在我的controller中使用的唯一function。

 public IActionResult Index()
    {
        List<OrderModel> orders = rapport.getByYear();
        return View(orders);
    }

我的索引视图

@model List<OrderModel>;

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
</div>

<div>
<table class="table" style="background-color:aliceblue;">
    <thead style="color: darkred;">
        <tr>
            <th>ID</th>
            <th>KlantId</th>
            <th>Datum</th>
            <th>Status</th>
            <th>TotaalPrijs</th>
        </tr>
    </thead>

    <tbody style="">
        @foreach (OrderModel order in Model) { 
            <tr>
                <td>@Html.DisplayFor(modelItem => order.BestellingId)</td>
                <td>@Html.DisplayFor(modelItem => order.KlantID)</td>
                <td>@Html.DisplayFor(modelItem => order.Datum)</td>
                <td>@Html.DisplayFor(modelItem => order.Status)</td>
                <td>@Html.DisplayFor(modelItem => order.Totaalprijs)</td>
            </tr>
        }
    </tbody>
</table>

您可以将动态查询编写为

public static string GetQuery(DateTime startDate, DateTime endDate)
{
    string query = @"select * from table
                    where dateCol > '" + startDate + @"' and dateCol < '" + endDate + @"'";

    return query;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM