如何在模型中传递List作为ajax调用的参数?

时间:2014-11-18 15:00:46

标签: c# jquery asp.net ajax asp.net-mvc

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

因此,通过我的ajax调用,我想改进搜索结果。但是,我通过在控制器中创建连接数据库并包含所有搜索过滤器的方法来实现此目的。然而这是不实际的,因为该方法包含一个巨大的开关盒,里面有各种开关盒。

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

这是我的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);
   }
 }

2 个答案:

答案 0 :(得分:2)

您可以通过首先根据year参数创建集合来简化控制器代码并删除外部开关

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);
}

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

答案 1 :(得分:1)

如果我正确地阅读了问题,您想要返回JsonResult并返回List<Persons>。然后,您将获得该结果并将其绑定到显示结果的任何内容:网格,列表等。

您可能希望查看某种客户端模板库以使其更容易。您当然可以使用jQuery来完成所有这些工作,但像handlebarsjs之类的东西会让它变得更容易。如果要在网格中显示结果,datatables.net非常好。