ASP.Net - 添加包含其他对象但包含的对象的新对象变为null

时间:2014-03-26 18:35:48

标签: c# asp.net asp.net-mvc asp.net-mvc-4 visual-studio-2013

我正在尝试将Course对象添加到我的数据库中,并且在我的表单中,我还有3个其他对象可以包含在Course对象中。在我的表单中,其他3个对象显示但是当它返回到控制器时,3个对象值为空。

如果需要更多信息,请告知我们。

为什么将Teacher,Student,GradedWork值返回为null?

我的控制器创建方法

public ActionResult Create()
    {
        var addForm = new CourseAddForm();

        addForm.Teachers = m.GetAllTeachersAsList();
        addForm.Students = m.GetAllStudentsAsList();
        addForm.GradedWorks = m.GetAllGradedWorkAsList();

        return View(addForm);
    }

    //
    // POST: /Course/Create
    [HttpPost]
    public ActionResult Create(CourseAdd newItem)
    {
        if (ModelState.IsValid)
        {

            var addedItem = m.AddCourse(newItem);

            if (addedItem == null)
            {
                return RedirectToAction("Index");
            }

            else
            {
                return RedirectToAction("Details", new { id = addedItem.Id });
            }


        }

        else
        {
            var addForm = Mapper.Map<CourseAddForm>(newItem);

            addForm.Teachers = m.GetAllTeachersAsList();
            addForm.Students = m.GetAllStudentsAsList();
            addForm.GradedWorks = m.GetAllGradedWorkAsList();

            return View(addForm);
        }
    }

我的观点模型

public class CourseAddForm : CourseAdd
{

    [Display(Name = "Teacher")]
    public ICollection<TeacherList> Teachers { get; set; }

    [Display(Name = "Student")]
    public ICollection<StudentList> Students { get; set; }

    [Display(Name = "Graded Works")]
    public ICollection<GradedWorkList> GradedWorks { get; set; }

    public CourseAddForm()
    {
        this.Teachers = new List<TeacherList>();
        this.Students = new List<StudentList>();
        this.GradedWorks = new List<GradedWorkList>();

    }



}

public class CourseAdd
{

    //[Key]
    [Display(Name = "Course Code")]
    public int CourseCode { get; set; }

    // [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

    //  [Required]
    [Display(Name = "Semester")]
    public string Semester { get; set; }

    // [Required]
    [Display(Name = "Section Id")]
    public string SectionId { get; set; }

    [Display(Name = "Teacher")]
    public ICollection<TeacherList> Teachers { get; set; }

    [Display(Name = "Student")]
    public ICollection<StudentList> Students { get; set; }

    [Display(Name = "Graded Works")]
    public ICollection<GradedWorkList> GradedWorks { get; set; }

}


public class CourseBase : CourseAdd
{
    [Key]
    public int Id { get; set; }

}


public class CourseBaseAddOn
{
    [Key]
    public int Id { get; set; }

    //[Key]
    [Display(Name = "Course Code")]
    public int CourseCode { get; set; }

    // [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

    //  [Required]
    [Display(Name = "Semester")]
    public string Semester { get; set; }

    // [Required]
    [Display(Name = "Section Id")]
    public string SectionId { get; set; }

    [Display(Name = "Teacher")]
    public ICollection<TeacherList> Teachers { get; set; }

    [Display(Name = "Student")]
    public ICollection<StudentList> Students { get; set; }

    [Display(Name = "Graded Works")]
    public ICollection<GradedWorkList> GradedWorks { get; set; }

}

我的经理方法

public CourseBaseAddOn AddCourse(CourseAdd newItem)
    {

        Course course = Mapper.Map<Course>(newItem);

        ds.Courses.Add(course);
        ds.SaveChanges();

        return Mapper.Map<CourseBaseAddOn>(course);

    }

我的表格

!(http://imgur.com/XkWRtES

2 个答案:

答案 0 :(得分:0)

当您填充集合并发送到视图时,视图显示数据和所有数据,当发送到客户端时,将被销毁。因此,您应该创建一个元素,以便在您提交表单时将数据发布到服务器。

答案 1 :(得分:0)

我在视图表单中忘记了name =“”属性,我能够找到ID,然后手动分配id == Id的对象