gpt4 book ai didi

javascript - Ajax 调用工作缓慢。可以让它变快吗?

转载 作者:行者123 更新时间:2023-11-29 21:37:56 25 4
gpt4 key购买 nike

当通过 JavaScript 和 ajax 在 asp.net mvc 中选择索引更改时,我正在填写“ReportToadd”下拉列表表单“ddlLanguage”下拉列表。这工作正常但需要很多时间来填写子下拉列表“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);

}

最佳答案

更新数据库查询的一种方法是不使用 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 传递给数据库,而不是在代码中执行。

关于javascript - Ajax 调用工作缓慢。可以让它变快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com