ahmed salah

ahmed salah

  • NA
  • 530
  • 142k

How to search using multiple field USING MVC

Dec 15 2016 5:26 PM

Problem

I need to search by ClassID,SectionID,InstructorID DROPDOWNLISTS and i can have options if one drop down have null value or not

search i need is (classID =NULL OR NOT)AND (sectionid =null or not)AND(InstructorID=Null or not)

I only can search by one drop down menu InstructorID BUT SECTIONID,ClassID Dropdown how to do that .

I have Model schedules as following :

  1. public class Schedule  
  2.     {  
  3.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  4.         [Key, Column(Order = 0)]  
  5.         public int SchedualeID { getset; }  
  6.        // [Key, Column(Order = 1)]  
  7.         public int CourseID { getset; }  
  8.         // [Key, Column(Order = 2)]  
  9.         [DisplayName("InstructorName")]  
  10.         public int InstructorID { getset; }  
  11.   
  12.         public int SectionID { getset; }  
  13.   
  14.         public int ClassID { getset; }  
  15.   
  16.         public int DayNum { getset; }  
  17.   
  18.         [DataType(DataType.Date)]  
  19.         [DisplayFormat(DataFormatString =  
  20.      "{0:dd-MM-yyyy}",  
  21.       ApplyFormatInEditMode = true)]  
  22.         [Required]  
  23.         public DateTime? FromDate { getset; }  
  24.         [DataType(DataType.Date)]  
  25.         [DisplayFormat(DataFormatString =  
  26.     "{0:dd-MM-yyyy}",  
  27.      ApplyFormatInEditMode = true)]  
  28.         [Required]  
  29.         public DateTime? ToDate { getset; }  
  30.   
  31.         [ForeignKey("ClassID")]  
  32.         public virtual Class classes { getset; }  
  33.         [ForeignKey("SectionID")]  
  34.         public virtual Section section { getset; }  
  35.         [ForeignKey("DayNum")]  
  36.         public virtual Calender calender { getset; }  
  37.         public virtual Instructor instructores { getset; }  
  38.         public virtual Courses courses { getset; }  
  39.     }  
  40. }  
 

This schedule model have foreign key for more tables as Class,Section,Instructor,Calender

All these tables have relations with table schedule

I need to get data from schedule based on ClassID,SectionID,InstructorID DROPDOWNLISTS

But i cannot do search by multiple fields

I only can search by InstructorID FIELD

What I try

Index action inside schedule controller

code below working but only InstructorID Drop down not all drop down

  1. public ActionResult Index()  
  2.         {  
  3.             ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID""InstructorName");  
  4.             var schedule = db.schedule.Include(s => s.calender).Include(s => s.classes).Include(s => s.courses).Include(s => s.instructores).Include(s => s.section);  
  5.           
  6.             return View(schedule.ToList());  
  7.         }  
  8.         [HttpPost]  
  9.         public ActionResult Index(int InstructorID)  
  10.         {  
  11.             ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID""InstructorName");  
  12.             var schedule = db.schedule.Include(s => s.calender).Include(s => s.classes).Include(s => s.courses).Include(s => s.instructores).Include(s => s.section).Where(s=>s.InstructorID==InstructorID);  
  13.             
  14.             return View(schedule.ToList());  
  15.         }  
In view of Index action in schedule controller
  1. <p>  
  2.     @Html.ActionLink("Create New", "Create")  
  3.     @using (Html.BeginForm())  
  4.     {  
  5.         <p>  
  6.             Title:@Html.DropDownList("InstructorID", null, htmlAttributes: new { @class = "form-control" })<br />  
  7.             <input type="submit" value="Filter" />  
  8.        </p>  
  9.     }  
  10. </p>  
 

Answers (1)