Hi I have courses table and its listing on index view , each course contains name,Program,block,Module and year , the listing is working fine now I want to search the courses based on any combinaiton of these parameters so below is my searching fields
public ActionResult Index(string sortOrder, string currentFilter, string SearchString,int? Block_Id,int? Program_Id,int? Year,int? Module_id, int? page)
{
ViewBag.Block_Id = new SelectList(db.Blocks, "Id", "Name");
ViewBag.Course_Category_Id = new SelectList(db.Courses_Category, "Id", "Category_Name");
ViewBag.Module_Id = new SelectList(db.Moduels, "Id", "Name");
ViewBag.Program_Id = new SelectList(db.Programs, "Id", "Program_Title");
ViewBag.Year = new SelectList(db.Years, "Id", "Name");
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (SearchString != null)
{
page = 1;
}
else
{
SearchString = currentFilter;
}
ViewBag.CurrentFilter = SearchString;
var course = from s in db.Courses
select s;
if (!String.IsNullOrEmpty(SearchString) || Block_Id != null || Program_Id != null || Year != null || Module_id != null)
{
course = course.Where(s => s.Course_Name.Contains(SearchString) || s.Program_Id==Program_Id || s.Block_Id == Block_Id || s.Module_Id==Module_id);
}
switch (sortOrder)
{
case "name_desc":
course = course.OrderByDescending(s => s.Course_Name);
break;
case "Date":
course = course.OrderBy(s => s.Date_Created);
break;
case "date_desc":
course = course.OrderByDescending(s => s.Date_Created);
break;
default:
course = course.OrderBy(s => s.Course_Name);
break;
}
int pageSize = 5;
int pageNumber = (page ?? 1);
ViewData["UserName"] = _ILogin.GetUserNamebyUserID(Convert.ToInt32(Session["UserID"]));
return View(course.ToPagedList(pageNumber,pageSize));
}
any one can share the complete search example with multiple columns few string parameters and few integer parameters
Gopaiah NekarukantiPosted Dec 3, 2019, 7:41 AM
Jenith ThakkarPosted Dec 3, 2019, 3:27 AM
Mark TaborPosted Dec 2, 2019, 11:16 AM
Jenith Thakkar yes I inspect that if i am not giving the parameter for text it matches with "" string to find the course , but after check your query why you made Block_Id==null and all other != null , I mean is it complete query or i need to writer for other parameters as well can you guide me .
Mark TaborPosted Dec 2, 2019, 11:11 AM
Jenith ThakkarPosted Dec 2, 2019, 6:03 AM
in that case you need to write seprate code
.
Gopaiah NekarukantiPosted Dec 2, 2019, 5:40 AM