none
How to search using multiple field using multi dropdown list RRS feed

  • Question

  • 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 :

     public class Schedule
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            [Key, Column(Order = 0)]
            public int SchedualeID { get; set; }
           // [Key, Column(Order = 1)]
            public int CourseID { get; set; }
            // [Key, Column(Order = 2)]
            [DisplayName("InstructorName")]
            public int InstructorID { get; set; }
    
            public int SectionID { get; set; }
    
            public int ClassID { get; set; }
    
            public int DayNum { get; set; }
    
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString =
         "{0:dd-MM-yyyy}",
          ApplyFormatInEditMode = true)]
            [Required]
            public DateTime? FromDate { get; set; }
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString =
        "{0:dd-MM-yyyy}",
         ApplyFormatInEditMode = true)]
            [Required]
            public DateTime? ToDate { get; set; }
    
            [ForeignKey("ClassID")]
            public virtual Class classes { get; set; }
            [ForeignKey("SectionID")]
            public virtual Section section { get; set; }
            [ForeignKey("DayNum")]
            public virtual Calender calender { get; set; }
            public virtual Instructor instructores { get; set; }
            public virtual Courses courses { get; set; }
        }
    }

    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

     
    public ActionResult Index()
            {
                ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "InstructorName");
                var schedule = db.schedule.Include(s => s.calender).Include(s => s.classes).Include(s => s.courses).Include(s => s.instructores).Include(s => s.section);
            
                return View(schedule.ToList());
            }
            [HttpPost]
            public ActionResult Index(int InstructorID)
            {
                ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "InstructorName");
                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);
              
                return View(schedule.ToList());
            }

    In view of Index action in schedule controller

    <p>
        @Html.ActionLink("Create New", "Create")
        @using (Html.BeginForm())
        {
            <p>
                Title:@Html.DropDownList("InstructorID", null, htmlAttributes: new { @class = "form-control" })<br />
                <input type="submit" value="Filter" />
           </p>
        }
    </p>


    Thursday, December 15, 2016 10:32 PM

All replies


  • Hi eng-ahmed-salah,

    You can try to use this "s.InstructorID == null ? true : s.InstructorID==InstructorID" format to query (True / false can filter null records).

    The following code for your reference.

       [HttpPost]
            public ActionResult Index(int InstructorID, int classID, int sectionid )
            {
                ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "InstructorName");
                var schedule = db.schedule.Include(s => s.calender).Include(s => s.classes).Include(s => s.courses).Include(s => s.instructores).Include(s => s.section).ToList();
    
                schedule .Where((s=>s.InstructorID == null ? true : s.InstructorID==InstructorID) && (s=>s.classID == null ? true : s.classID==classID)  && (s=>s.sectionid == null ? true : s.sectionid==sectionid) ) ;
              
                return View(schedule.ToList());
            }
    

    If I have any misunderstanding, you can include all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 16, 2016 9:03 AM
    Moderator