mvc ile DropDownList oluşturmak -sample-

// aşağıdaki KOD kurs tanımları index sayfasınına ait..
@using (Html.BeginForm())
{
    <p>
        Department Seçin: @Html.DropDownList("SelectedDepartment", "Tümü")
        <input type="submit" value="Filter" />
    </p>
}

// aşağıdaki actionresult controller da.
private SchoolContext db = new SchoolContext();

        // GET: Course
        public ActionResult Index(int? SelectedDepartment)
        {
            var departments = db.Departments.OrderBy(dep => dep.Name).ToList();
            ViewBag.SelectedDepartment = new SelectList(departments, "DepartmentID", "Name", SelectedDepartment);
            int departmentID = SelectedDepartment.GetValueOrDefault();

            IQueryable<Course> courses = db.Courses
                .Where(c => !SelectedDepartment.HasValue || c.DepartmentID == departmentID)
                .OrderBy(d => d.CourseID)
                .Include(d => d.Department);
            var sql = courses.ToString();
            return View(courses.ToList());
        }
//..................................................


Yorumlar