ASP.NET MVC:每个项目的复选框,然后收集变量中的选定项目?

时间:2015-02-23 17:57:39

标签: c# asp.net asp.net-mvc asp.net-mvc-4

在我的Contoso大学MVC项目中的一个索引页面Course/Index上,我为每个Course项添加了一个复选框,还有一个按钮:“检索学生”,点击后,检索所有已注册所选课程的学生。当我单击“检索学生”时,我已成功重定向程序执行以在我的Controller内调用Action:StudentsEnrolledInSelectedCourses ()内部:CourseController.cs。现在我正在尝试在StudentsEnrolleInSelectedCourses ()中添加逻辑以检索所选的所有课程项目,并检索所有已注册所选课程的学生。但是,我不知道检查课程项目是否已被选中的正确方法。我在课程模型:Course.cs中添加了一个“Selected”字段,但是,当用户选择课程时,数据库中的值不会更新。我在SQL Server对象资源管理器中检查了这个。实现Checkbox功能的正确方法是什么,以及何时检查项目是否已被选中/选择?

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>

0 个答案:

没有答案