Marijn
Marijn

Reputation: 79

how to pass a List in the model as a parameter of an ajax call?

i use MVC 4 ASP.net with Razor views.

So with my ajax call i want to refine my search results. However atm i do this by creating methods in the controller that connect with the database and include ALL the search filters. This is not practical however because the method contains a huge switch case with all sorts of switch cases inside.

This is not practical so my qeustion is how can i retrieve the table information runtime in the View? i know how to pass parameters with my AJAX method and have used this to access a method that goes to the database. It's however better to get the current view his list and modify that one. Basically i want to change all the database calls to the list i get in the parameter so i can adjust stuff in that list rather than connecting to the database after each filter i just have no clue how to pass on the current list in the view

here's my AJAX call:

$(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?");
      }
    });
  }

and here's my method which is WAY too long:

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

Upvotes: 1

Views: 315

Answers (2)

user3559349
user3559349

Reputation:

You could simplify the controller code and delete the outer switch by first creating a collection based on the year parameter

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

Note also you could store the unfiltered Persons collection in session in the initial GET method and use that rather than making a database call each time.

Upvotes: 2

Tim Hobbs
Tim Hobbs

Reputation: 2017

If I am reading the question correctly you want to return a JsonResult and just return a List<Persons>. You'll then get that result and bind it to whatever you are displaying the results in: a grid, list, etc.

You'll likely want to look into some sort of client templating library to make this easier. You can certainly use jQuery to do all this, but something like handlebarsjs would make it much easier. If you are displaying results in a grid, datatables.net is pretty nice.

Upvotes: 1

Related Questions