gpt4 book ai didi

c# - 如何将模型中的列表作为 ajax 调用的参数传递?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:56 25 4
gpt4 key购买 nike

我使用带有 Razor View 的 MVC 4 ASP.net。

所以我想通过我的 ajax 调用来优化我的搜索结果。但是 atm 我通过在 Controller 中创建与数据库连接并包括所有搜索过滤器的方法来做到这一点。然而,这是不切实际的,因为该方法包含一个巨大的 switch case,里面有各种 switch case。

这不切实际,所以我的问题是如何在 View 中检索表信息运行时?我知道如何使用我的 AJAX 方法传递参数,并使用它来访问进入数据库的方法。然而,最好获取当前 View 他的列表并修改该列表。基本上我想将所有数据库调用更改为我在参数中获得的列表,这样我就可以调整该列表中的内容而不是在每个过滤器之后连接到数据库我只是不知道如何传递 View 中的当前列表

这是我的 AJAX 调用:

$(checkbox5yearsfilter).click(function () {
if ($(checkbox5yearsfilter).is(':checked')) {
$.ajax({
type: 'POST',
url: '../Person/changeTableOnWorkExp',
datatype: 'html',
data: { parameter: $('#parameter').val(), choice: $('#choice').val(), years: 5, ischecked: "yes" },
success: function (data) {
$('span div.searchresults').html(data);
},
error: function (data) {
alert("Something went wrong with the call, have you searched already?");
}
});
}

这是我的方法,但太长了:

public PartialViewResult changeTableOnWorkExp(string parameter, string choice, int years, string ischecked)
{
List<Person> list = new List<Person>();
if (ischecked == "yes")
{
switch (years)
{
case 5:
switch(choice)
{
case "Knows Already":
list = (from p in db.Persons where p.YearsOfWorkExperience > 5 && (p.HobbyProjectICTRelated.Contains(parameter) || p.LearntSkillsAndLevelOfSkills.Contains(parameter)) select p).ToList();
return PartialView("_SearchSkills", list);
case "Wants To Learn":
list = (from p in db.Persons where p.YearsOfWorkExperience > 5 && p.SkillsToLearn.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Hobbies":
list = (from p in db.Persons where p.YearsOfWorkExperience > 5 && p.Hobbys.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Name":
list = (from p in db.Persons where p.YearsOfWorkExperience > 5 && p.Name.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
}
break;
case 10:
switch(choice)
{
case "Knows Already":
list = (from p in db.Persons where p.YearsOfWorkExperience > 10 && (p.HobbyProjectICTRelated.Contains(parameter) || p.LearntSkillsAndLevelOfSkills.Contains(parameter)) select p).ToList();
return PartialView("_SearchSkills", list);
case "Wants To Learn":
list = (from p in db.Persons where p.YearsOfWorkExperience > 10 && p.SkillsToLearn.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Hobbies":
list = (from p in db.Persons where p.YearsOfWorkExperience > 10 && p.Hobbys.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Name":
list = (from p in db.Persons where p.YearsOfWorkExperience > 10 && p.Name.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
}
break;
case 15:
switch(choice)
{
case "Knows Already":
list = (from p in db.Persons where p.YearsOfWorkExperience > 15 && (p.HobbyProjectICTRelated.Contains(parameter) || p.LearntSkillsAndLevelOfSkills.Contains(parameter)) select p).ToList();
return PartialView("_SearchSkills", list);
case "Wants To Learn":
list = (from p in db.Persons where p.YearsOfWorkExperience > 15 && p.SkillsToLearn.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Hobbies":
list = (from p in db.Persons where p.YearsOfWorkExperience > 15 && p.Hobbys.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
case "Name":
list = (from p in db.Persons where p.YearsOfWorkExperience > 15 && p.Name.Contains(parameter) select p).ToList();
return PartialView("_SearchSkills", list);
}
break;
default:
list = null;
return null;
}
return null;
}
else
{
list = getListPerson(parameter, choice);
return PartialView("_SearchSkills", list);
}
}

最佳答案

您可以通过首先创建一个基于year 参数的集合来简化 Controller 代码并删除外部开关

public PartialViewResult changeTableOnWorkExp(string parameter, string choice, int years, string ischecked)
{
var list = (from p in db.Persons where p.YearsOfWorkExperience > years select p).AsEnumerable();
switch(choice)
{
case "Knows Already":
list = list.Where(p => p.HobbyProjectICTRelated.Contains(parameter) || p.LearntSkillsAndLevelOfSkills.Contains(parameter));
case "Wants To Learn":
....
}
return PartialView("_SearchSkills", list);
}

另请注意,您可以将未过滤的 Persons 集合存储在初始 GET 方法的 session 中并使用它,而不是每次都调用数据库。

关于c# - 如何将模型中的列表作为 ajax 调用的参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997362/

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