Ajax调用工作很慢。可以快速完成吗?

时间:2015-12-16 07:47:39

标签: javascript c# jquery asp.net ajax

我正在填写" ReportToadd"下拉列表表格" ddlLanguage"当选择索引通过JavaScript和ajax在asp.net mvc中更改时的下拉列表。这工作正常,但需要花费很多时间来填充子项下载" ReportToadd"。需要4到5秒来填充第二个下拉列表。如何快速完成。请帮助并提前感谢

JavaScript代码:

$(function () {
    $('select#ddlLanguage').change(function () {
        var languageId = $(this).val();            
        var projectType ='@(TempData["projectType"])';    
        $.ajax({
            url: "/SEI/Report/FillReport",
            type: 'POST',
            data: JSON.stringify({ languageId: languageId, projectType: projectType }),
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                $("#ReportToAdd").html("");
                $.each(data, function (key, result) {
                    $('select#ReportToAdd').append(
                        '<option value="' + result.Value + '">'
                        + result.Text +
                        '</option>');
                });
            }
        });
    });
});

和下拉列表是:

        string projectType = "SEI_ADULT";                                              
        @Html.DropDownList("ddlLanguage", SixSeconds.Utils.SelectList<SixSeconds.Models.Language>("Name", ""), new { @id = "ddlLanguage", style = "width:300px;" }) <br /> 
        TempData["projectType"] = projectType;                                                 
        @Html.DropDownList("ReportToAdd", Enumerable.Empty<SelectListItem>(), new { @id = "ReportToAdd", style = "width:300px;" })

和Json方法就像

 public JsonResult FillReport(int languageId,string projectType,string selectedValue, bool showCredits = true)
    {
        DataAccessObject<ReportType> dao = new DataAccessObject<ReportType>();
        DataAccessObject<Language> ldao = new DataAccessObject<Language>();
        //IEnumerable<ReportType> list = criteria != null ? dao.Filter(criteria) : dao.All().ToList();
        IEnumerable<ReportType> list = dao.All().ToList();
        IEnumerable<Language> Llist = ldao.All().ToList();

        list = list.Where(a => a.ProjectType.ToString() == projectType).ToList();
        list = list.OrderBy(r => r.CustomOrder);

        List<SelectListItem> result = new List<SelectListItem>();
        result.Add(new SelectListItem() { Value = "", Text = "" });
        foreach (ReportType t in list)
            {
            foreach (Language l in t.Languages.Where(a=>a.Id==languageId).ToList())
            {               
                    string displayText = t.Name + " (" + l.Name + ")" + (showCredits ? " - " + (t.Code == "BTP" ? 10 : t.Credits) + " " + App_GlobalResources.FieldLabels.Credits : "");
                    string value = t.Id + "-" + l.Id + "-" + (t.Code == "BTP" ? 10 : t.Credits) + "-" + t.Code + "-" + l.Code.Replace("-", "_");
                    result.Add(new SelectListItem() { Selected = (selectedValue == value), Value = value, Text = displayText });
             }
        }
        return Json(result);

    }

1 个答案:

答案 0 :(得分:2)

更新数据库查询的一种方法是不使用All并传递Where

此外,永远不会使用Llist,因此您无需从数据库中获取所有语言。

尝试: -

//IEnumerable<Language> Llist = ldao.All().ToList();
IEnumerable<ReportType> list = dao.Where(a => a.ProjectType.ToString() == projectType)
                                   .OrderBy(r => r.CustomOrder).ToList();

而不是: -

IEnumerable<ReportType> list = dao.All().ToList();
IEnumerable<Language> Llist = ldao.All().ToList();

list = list.Where(a => a.ProjectType.ToString() == projectType).ToList();
list = list.OrderBy(r => r.CustomOrder);

这会将where和order by传递给db,而不是在代码中执行。