DBContext不保存修改的条目

时间:2015-05-06 04:48:59

标签: c# entity-framework asp.net-mvc-4 razor

我正在开发一个有学生的简单网站,并且有课程。有趣的是,我似乎无法为我的学生添加课程并将其保存。我调用savechanges()方法,但在我的cshtml页面上,我的viewbag不包含更新的学生。这就是我的意思。

enter image description here

在这里,您可以看到我的ViewBag.Student包含一个有效的学生,该学生已被修改为包含PendingCourses下的两门课程。但是,当我加载我的cshtml页面时,我的ViewBag.Student包含:

enter image description here

所以尽管我无法弄清楚它是什么,但肯定会有一些脱节。所以基本上我的Details页面通过在上下文中执行where语句来设置ViewBag。使用AddCourseToStudent函数修改并保存此上下文,然后在cshtml中读取ViewBag.Student。任何帮助深表感谢。以下是我的文件供参考。

详情功能:

// GET: Student/Details/5
    [Authorize]
    public ActionResult Details(int id)
    {
         using (var context = new StudentDbContext())
         {
             ViewBag.Student = context.Students.Where(u => u.id == id).FirstOrDefault();
         }
         var context2 = new ApplicationDbContext();
         var userList = context2.Users.OrderBy(r => r.UserName).ToList().Select(rr => new SelectListItem { Value = rr.UserName.ToString(), Text = rr.UserName }).ToList();
         ViewBag.Users = userList;
        return View();
    }

CSHTML:

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
    SchoolRegistration.Models.Student student = ViewBag.Student;
    List<SchoolRegistration.Models.Course> completedCourses = student.CompletedCourses;
    List<SchoolRegistration.Models.Course> pendingCourses = student.PendingCourses;
    List<SchoolRegistration.Models.Course> failedCourses = student.FailedCourses;
    List<int> yearRange = new List<int>();
    if (completedCourses != null) {
        foreach (SchoolRegistration.Models.Course course in completedCourses)
        {
            if (!yearRange.Contains(course.Year))
            {
                yearRange.Add(course.Year);
            }
        }
    }
    if (pendingCourses != null)
    {
        foreach (SchoolRegistration.Models.Course course in pendingCourses)
        {
            if (!yearRange.Contains(course.Year))
            {
                yearRange.Add(course.Year);
            }
        }
    }
    if (failedCourses != null)
    {
        foreach (SchoolRegistration.Models.Course course in failedCourses)
        {
            if (!yearRange.Contains(course.Year))
            {
                yearRange.Add(course.Year);
            }
        }
    }
    yearRange.Sort();
}

<h2>Details</h2>
@if (TempData["Success"] != null)
{
    if (TempData["Success"] == "nocourse")
    {
        <div style="background-color:aliceblue">
            <p><strong>Success:</strong><a href="#" id="createCourseLink">Create Course?</a></p>
        </div>
    }
    else
    {
        <div style="background-color:aliceblue">
            <p><strong>Success:</strong> @TempData["Success"].ToString()</p>
        </div>
    }
}

<h3>@student.FirstName @student.LastName - @student.StudentID</h3>

<h4>@student.GradeLevel - @student.ExpectedGraduation</h4>

<h3>Degree Audit</h3>
@using (Html.BeginForm("GetCourse", "Student", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <p>Start Year: @Html.DropDownList("startYear", (IEnumerable<SelectListItem>)yearRange.ToList().Select(rr => new SelectListItem { Value = rr.ToString(), Text = rr.ToString() }).ToList(), "Select...") Start Semester: @Html.DropDownList("startSemester", EnumHelper.GetSelectList(typeof(SchoolRegistration.Models.Semesters)), "Select...")</p>
    <p>End Year: @Html.DropDownList("endYear", (IEnumerable<SelectListItem>)yearRange.ToList().Select(rr => new SelectListItem { Value = rr.ToString(), Text = rr.ToString() }).ToList(), "Select...") End Semester: @Html.DropDownList("endSemester", EnumHelper.GetSelectList(typeof(SchoolRegistration.Models.Semesters)), "Select...")</p>

    <input type="submit" value="Search" id="btnStudentFilter" />
}

<h3>Course Management</h3>
@using (Html.BeginForm("GetCourseForStudent", "Student", FormMethod.Post))
{
    @Html.AntiForgeryToken();
    <hr />
    <p><strong>Subject: </strong>@Html.DropDownList("cat", EnumHelper.GetSelectList(typeof(SchoolRegistration.Models.Categories)), "Select...")</p>
    <p><strong>CAT NO: </strong>@Html.TextBox("CourseId")</p>
    <p><strong>Section: </strong>@Html.TextBox("SectionId")</p>
    <p><strong>Year: </strong>@Html.TextBox("Year")</p>
    <p><strong>Semester: </strong>@Html.DropDownList("semester", EnumHelper.GetSelectList(typeof(SchoolRegistration.Models.Semesters)), "Select...")</p>
    <input type="submit" value="Search" />
}

@if (TempData["RetrievedCourses"] != null)
{
    List<SchoolRegistration.Models.Course> courses = (List<SchoolRegistration.Models.Course>)TempData["RetrievedCourses"];
    for (int i = 0; i < courses.Count; i++)
    {
        <p>@courses[i].Name - @courses[i].Category @courses[i].CourseId . @courses[i].SectionId - @courses[i].Credits Credits | @Html.ActionLink("Add to Student", "AddCourseForStudent", "Student", new { courseId = courses[i].id, studentId = student.id},null)</p>
    }
    <p>Course Search DIV Link</p>
}

AddCourseToStudent:

public ActionResult AddCourseForStudent(int courseId, int studentId)
    {
        using (var context = new StudentDbContext())
        {
            Student student = context.Students.Where(u => u.id == studentId).FirstOrDefault();
            Course course = context.Courses.Where(u => u.id == courseId).FirstOrDefault();

            List<Course> completed = student.CompletedCourses;
            List<Course> pending = student.PendingCourses;
            List<Course> failed = student.FailedCourses;

            if (completed == null)
                completed = new List<Course>();
            student.CompletedCourses = completed;
            if (pending == null)
                pending = new List<Course>();
            student.PendingCourses = pending;
            if (failed == null)
                failed = new List<Course>();
            student.FailedCourses = failed;
            pending.Add(course);
            try
            {
                context.Entry(student).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
                ViewBag.Student = student;
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
        TempData["Success"] = "Courses added to student.";

        return Redirect(Request.UrlReferrer.ToString());
    }

型号:

public class InstructorViewModels
{
}

public enum GradeLevels
{
    Freshman,
    Sophmore,
    Junior,
    Senior,
    Graduate
}

public enum Categories
{
    CSCI
}

public enum Semesters
{
    FA,
    SP,
    S1,
    S2,
    S3,
    IN
}

public enum CourseTypes
{
    Humanities
}

public class Student
{
    public int id { get; set; }
    [Required]
    public int StudentID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    [Required]
    public GradeLevels GradeLevel { get; set; }
    [Required]
    public List<Course> CompletedCourses { get; set; }
    [Required]
    public List<Course> PendingCourses { get; set; }
    [Required]
    public List<Course> FailedCourses { get; set; }
    [Required]
    public int ExpectedGraduation { get; set; }
}

public class Course
{
    public int id { get; set; }
    public int CourseId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int SectionId { get; set; }
    [Required]
    public Categories Category { get; set; }
    [Required]
    public int Credits { get; set; }
    [Required]
    public Semesters Semester { get; set; }
    [Required]
    public int Year { get; set; }
    [Required]
    public string InstructorId { get; set; }
    public CourseTypes CourseType { get; set; }
}

数据库:

enter image description here

enter image description here

0 个答案:

没有答案