ASP.Net -添加一个包含其他对象的新对象,但被包含的对象变为空
本文关键字:对象 包含 新对象 其他 Net 添加 一个 ASP | 更新日期: 2023-09-27 17:53:35
我试图添加一个课程对象到我的数据库,在我的形式,我有3个其他对象,可以包含在课程对象。在我的表单中,其他3个对象显示,但当它返回到控制器时,3个对象值为null。
如果需要更多的信息,请告诉我。
为什么Teacher, Student, GradedWork值返回为空?
我的控制器创建方法
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);
}
}
My view models
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; }
}
My manager method
public CourseBaseAddOn AddCourse(CourseAdd newItem)
{
Course course = Mapper.Map<Course>(newItem);
ds.Courses.Add(course);
ds.SaveChanges();
return Mapper.Map<CourseBaseAddOn>(course);
}
我的形式! (https://i.stack.imgur.com/c7abx.jpg)
当你填充你的集合并发送给视图时,视图显示的数据和所有的数据,当发送给客户端时,将被销毁。因此,您应该创建一个元素来保存提交表单时要提交给服务器的数据。
我忘记了view表单中的name="属性,这样我就可以找到ID,然后手动分配ID == ID的对象