gpt4 book ai didi

c# - 从 Controller 在模型中传递 ICollection 时,Ajax 方法将不再起作用

转载 作者:行者123 更新时间:2023-11-30 23:17:54 26 4
gpt4 key购买 nike

我正在尝试列出特定学生的所有类(class)。但是,我在将 ICollection 列表发送到 Ajax 方法时遇到问题。当我这样做时,没有任何反应,我收到错误消息(在 chrome 中):

http://localhost:25237/Home/Test 500 (Internal Server Error)

如果我评论/删除 Courses = readStudent.Courses(CoursesICollection),我的 Ajax 方法就会按预期工作(没有培训类)。

public ActionResult Test(Guid id) {

var readStudent = db.Students.SingleOrDefault(x => x.Id == id);

var results = new StudentVm {

FirstName = readStudent.FirstName,
LastName = readStudent.LastName,
SchoolName = readStudent.School.Name,
Courses = readStudent.Courses //Icollection, works if I remove this line.
};

return Json(results);
}

我的 Ajax 代码:

 $.post("/Home/Test", { "id": id },
function (data) {
if (data) {

$("#testDiv").append($('<div class=\"studentInfo\"> ' +
'<br/> ' +
<b>Student: </b>' + data.FirstName + " " + data.LastName +
'<br/>' +
'<b>School: </b>' + data.SchoolName +
'<br/>' +
'<b>All courses: </b>' +

$.each(data.Courses(function(index, item) { allCources += '<b>' + item + '</b>'; })

+'</div>'));

}
});

在 View 中,我需要访问 CourseNameDescription 属性

有什么想法吗?

编辑:

System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Student_32DDE9D84FE99EFF8907954351AD1F1A8162A0B565198DD8C7B4B25E2330BB98'.

学生模型(英孚创建):

public partial class Student {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Student() {
this.Courses = new HashSet<Course>();
}

public System.Guid Id { get; set; }
public System.Guid SchoolId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime Started { get; set; }
public Nullable<System.DateTime> Ended { get; set; }

public virtual School School { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Course> Courses { get; set; }
}

类(class)模型(英孚创建)

  public partial class Course {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Course() {
this.Students = new HashSet<Student>();
}

public System.Guid ID { get; set; }
public string Name { get; set; }
public string Classroom { get; set; }
public string Description { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Student> Students { get; set; }
}

我的 ViewModel 传递给 View:

public class StudentVm {

public string FirstName { get; set; }
public string LastName { get; set; }
public string SchoolName { get; set; }

public virtual ICollection<Course> Courses { get; set; }

}

最佳答案

异常被抛出是因为 Course 包含一个 Student 的属性 - JavascriptSerializer 试图序列化 Course包含 Student,其中包含 Course,其中包含 Student 等等(如果继续,您将耗尽内存)。

您只需要返回 View 中需要的Course 属性,因此StudentVMCourses 属性应该是一个集合另一个 View 模型仅包含您需要的属性,或者只是将匿名对象传递回客户端

var student = db.Students.SingleOrDefault(x => x.Id == id);
var results = new
{
FirstName = student.FirstName,
LastName = student.LastName,
SchoolName = student.SchoolName,
Courses = student.Courses.Select(x => new
{
Name = x.Name,
Description = x.Description
})
};
return Json(results);

或者,如果你想使用 View 模型,那么它们将需要

public class StudentVm
{
public string FirstName { get; set; }
....
IEnumerable<CourseVM> Courses { get; set; }
}
public class CourseVM
{
public string Name { get; set; }
public string Description { get; set; }
}

Controller 代码将是

var student = db.Students.SingleOrDefault(x => x.Id == id);
var results = new StudentVm
{
FirstName = student.FirstName,
LastName = student.LastName,
SchoolName = student.School.Name,
Courses = student.Courses.Select(x => new StudentVM
{
Name = x.Name,
Desription = x.Name
}
};
return Json(results);

然后在 View 中,您需要更改 $.each() 循环中的代码以访问 Courses

的每个属性
$.each(data.Courses(function(index, item) {
allCources += '<b>' + item.Name + '</b>'; // and item.Description
})

或者,考虑使用更好更强大的 Json.NET用于序列化数据,它具有设置如何处理循环引用的选项。

关于c# - 从 Controller 在模型中传递 ICollection 时,Ajax 方法将不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com