【问题标题】:ASP.NET Best Practice for Table表的 ASP.NET 最佳实践
【发布时间】:2020-04-19 18:24:37
【问题描述】:

我正在处理一个 ASP.NET Core 项目,该项目有一个表,我根据它们所在的当前选项卡对其进行过滤。我最初的想法是使用 JSON 调用 C# 并获取表格的合格项列表。

我想到的下一个计划是获取所有合格项目的列表,然后在 for 循环中使用过滤器?

    public JsonResult OnGetGetSetups(string condition,int vehID)
    {
        if (vehID == 0)
            return null;

        switch (condition)
        {
            case "Dirt":
                return new JsonResult(_context.Setups.Include(d => d.Driver).Include(sd => sd.SetupDetails).Include(c => c.SetupDetails.Condition).Where(a => a.SetupDetails.Condition.Name == condition && a.VehicleId == vehID).ToList());
            case "Carpet":
                return new JsonResult(_context.Setups.Include(d => d.Driver).Include(sd => sd.SetupDetails).Include(c => c.SetupDetails.Condition).Where(a => a.SetupDetails.Condition.Name == condition && a.VehicleId == vehID).ToList());
            default:
                List<Setup> tmp = _context.Setups.Include(d => d.Driver).Include(sd => sd.SetupDetails).Include(c => c.SetupDetails.Condition).Where(a => a.VehicleId == vehID).ToList();
                return new JsonResult(tmp);
        }
    }

然后我有 3 个事件监听器调用这个 GetSetups。这似乎是解决这个问题的一种非常混乱的方法。

有没有更好的方法来解决这个问题?

类似的东西:

$(function () {
    $("#allSetups").on("click", function () {
        if (activeTab == "allSetups") {
            return;
        }
        activeTab = "allSetups";

        $("#infoArea").empty();
        var vehId = document.getElementById('vehicleId').value;
        var url2 = '@Url.Page("/setups/search","GetSetups")';
        $.getJSON(url2, { condition: "all", vehID: vehId }, (data) => {
            $.each(data, function (i, item) {
                $("#vehcileId").append(`<option value="${item.vehicleId}">${item.name}</option>`);
            });
        });
    });
});

【问题讨论】:

    标签: c# jquery asp.net json performance


    【解决方案1】:

    你可以试试这个方法:

        public JsonResult OnGetGetSetups(string condition, int vehID)
        {
            if (vehID == 0)
                return null;
    
            return new JsonResult(_context.Setups.Include(d => d.Driver)
                                                 .Include(sd => sd.SetupDetails)
                                                 .Include(c => c.SetupDetails.Condition)
                                                 .Where(a => a.VehicleId == vehID
                                                            && ((condition != "Dirt" && condition != "Carpet") || a.SetupDetails.Condition.Name == condition))
                                                 .ToList());
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多