0

On one of my index pages: Course/Index in my Contoso University MVC project, I've added a checkbox for each Course item, and a button: "Retrieve Students" that when clicked, will retrieve all Students who are enrolled in the selected Courses. I've successfully redirected the program execution to call inside Action: StudentsEnrolledInSelectedCourses () inside my Controller: CourseController.cs when I click "Retrieve Students". Now I'm trying to add logic in StudentsEnrolleInSelectedCourses () to retrieve all the Course items selected, and retrieve all Students who are enrolled in the selected Courses. However, I don't know the correct way to check if a Course item has been selected. I've added a "Selected" field in the Course Model: Course.cs, however, this value is not updated in the database when the user selects the Course. I checked this in the SQL Server Object Explorer. What's the correct way to implement a Checkbox feature, and when to check if an item has been checked/selected?

CourseController.cs:

namespace ContosoUniversity.Controllers
{


 public class CourseController : Controller
    {
        private SchoolContext db = new SchoolContext();

        public void StudentsEnrolledInSelectedCourses ()
        {
            //Iterate thru Each Course Item, 
            //if it's selected add to list?
            //Does act of selecting a Course item update its Selected bool?
            //Must manually update?

            IQueryable<Course> courses = db.Courses
            .Where(c => c.Selected == true)
            .OrderBy(d => d.CourseID)
            .Include(d => d.Title);


            foreach (var c in courses)
            {
                Debug.WriteLine(c.Title);
                Debug.WriteLine(c.Selected);
            }
        }
         ...
    }
 }



Course.cs:

namespace ContosoUniversity.Models
{
    public class Course
    {

        [Display(Name = "Select")]
        public bool Selected { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Name = "Number")]
        public int CourseID { get; set; }

        [StringLength(50, MinimumLength = 3)]
        public string Title { get; set; }

        [Range(0, 5)]
        public int Credits { get; set; }

        public int DepartmentID { get; set; }



        public virtual Department Department { get; set; }
        public virtual ICollection<Enrollment> Enrollments { get; set; }
        public virtual ICollection<Instructor> Instructors { get; set; }
    }
}

Views/Course/Index.cshtml:

@model IEnumerable<ContosoUniversity.Models.Course>

@{
    ViewBag.Title = "Courses";
}

<h2>Courses</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm())
{
    <p>
        Select Department: @Html.DropDownList("SelectedDepartment", "All")
        <input type="submit" value="Filter" />
    </p>
}

@using (Html.BeginForm("StudentsEnrolledInSelectedCourses", "Course", FormMethod.Get))
{
    <p>
        Retrieve All Students taking Selected Courses
        <input type="submit" value="Retrieve Students"/>
    </p>
}

<table class="table">
    <tr>
        <th>
           Select
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CourseID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Credits)
        </th>
        <th>
            Department
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.CheckBoxFor(modelItem => item.Selected)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CourseID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Credits)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Department.Name)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.CourseID }) |
                @Html.ActionLink("Details", "Details", new { id = item.CourseID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.CourseID })
            </td>
        </tr>
    }

</table>
jerryh91
  • 1,777
  • 10
  • 46
  • 77
  • below is link for model list binding. click and learn / – Shahzad Khan Feb 23 '15 at 18:12
  • possible duplicate of [CheckboxList in MVC3.0](http://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0) – Rob Tillie Feb 23 '15 at 18:13
  • This question has an excellent answer: http://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0 – Rob Tillie Feb 23 '15 at 18:14
  • You need a `for` loop (not `foreach` which is creating invalid html and giving all the checkboxes the same `name` and `id` attributes). `for(int i = 0; i < Model.Count; i++) { @Html.CheckboxFor(m => m[i].Selected) ...}` –  Feb 23 '15 at 21:48

0 Answers0