我正在尝试返回教育系统中的时间表列表。我的项目中有一个具有此属性的计划模型:
public partial class Schedule
{
public Schedule()
{
this.ClassTimes = new HashSet<ClassTime>();
this.Scores = new HashSet<Score>();
}
public int Id { get; set; }
public int TeacherId { get; set; }
public int LessonId { get; set; }
public int ClassId { get; set; }
public int DegreeId { get; set; }
public int FacultyId { get; set; }
public int SemesterId { get; set; }
public int MajorId { get; set; }
public System.DateTime DateOfExame { get; set; }
public string Capacity { get; set; }
public string locationOfExame { get; set; }
public virtual Class Class { get; set; }
public virtual ICollection<ClassTime> ClassTimes { get; set; }
public virtual Degree Degree { get; set; }
public virtual Faculty Faculty { get; set; }
public virtual Lesson Lesson { get; set; }
public virtual Major Major { get; set; }
public virtual ICollection<Score> Scores { get; set; }
public virtual Semester Semester { get; set; }
public virtual Teacher Teacher { get; set; }
}
所以在这个模型中,我保存了我的实体的 id
,例如专业、教师、类(class)等。所以我需要返回我的日程表列表。所以我必须将实体的 id
转换为该实体的名称。所以我在我的 MVC 项目中设计了一个调度 Controller ,如下所示:
private readonly ScheduleRepositor obj = new ScheduleRepositor();
public ActionResult Index()
{
var list=new List<SchedulePresentation>();
ClassRepository objclassrep=new ClassRepository();
DegreeRepositor objdegreerep=new DegreeRepositor();
FacultyRepositor objfactulyrep=new FacultyRepositor();
LessonRepository objLessonRep=new LessonRepository();
MajorRepository objmajorrep=new MajorRepository();
SemesterRepositor objsemesterrep=new SemesterRepositor();
TeacherRepositor objteacherrep=new TeacherRepositor();
DateConverter objdateconverte = new DateConverter();
List<Schedule> model = obj.GetLessonlist();
foreach (var t in model)
{
SchedulePresentation objpres=new SchedulePresentation();
objpres.Capacity = t.Capacity;
objpres.DateOfExam = objdateconverte.ConvertToPersianToShow(t.DateOfExame);
objpres.className = objclassrep.FindClassById(t.ClassId).ClassName;
objpres.degreeName = objdegreerep.FindDegreeById(t.DegreeId).DegreeName;
objpres.examLocation = t.locationOfExame;
objpres.facultyName = objfactulyrep.FindFacultyById(t.FacultyId).FacultyName;
objpres.lessonName = objLessonRep.FindLessonById(t.LessonId).LessonName;
objpres.majorName = objmajorrep.FindMajorById(t.MajorId).MajorName;
objpres.semesterName = objsemesterrep.FindSemesterById(t.SemesterId).SemesterName;
objpres.teacherName = objteacherrep.FindTeacherById(t.TeacherId).Name + " " +
objteacherrep.FindTeacherById(t.TeacherId).LastName;
list.Add(objpres);
}
return View(list);
}
所以我只是为每个实体创建一个存储库,以通过 id 返回我的实体的名称。
然后我为我的日程表创建了一个表示类,将 id
转换为我的实体名称,如下所示:
public class SchedulePresentation
{
public string teacherName { set; get; }
public string lessonName { set; get; }
public string className { set; get; }
public string degreeName { set; get; }
public string facultyName { set; get; }
public string semesterName { set; get; }
public string majorName { set; get; }
public string DateOfExam { set; get; }
public string Capacity { set; get; }
public string examLocation { set; get; }
}
所以我有两个问题。我的解决方案中有 4 个项目:
- 领域类
- 模型
- 存储库
- MVC项目
所以
我将这些 Id 转换为它们在 MVC 层中的名称是一个好方法,还是为这个时间表列表制作一个 Repository 或创建一个模型更好?
在最后一行,当我想返回我的列表时,我得到了这个错误:
传入字典的模型项的类型为“System.Collections.Generic.List
1[EducationMVC.PresentationClass.SchedulePresentation]”,但此字典需要一个类型为“System.Collections.Generic”的模型项。 IEnumerable
1[DomainClasses.Schedule]'。
MVC的 View 代码:
@model IEnumerable<DomainClasses.Schedule>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.TeacherId)
</th>
<th>
@Html.DisplayNameFor(model => model.LessonId)
</th>
<th>
@Html.DisplayNameFor(model => model.ClassId)
</th>
<th>
@Html.DisplayNameFor(model => model.DegreeId)
</th>
<th>
@Html.DisplayNameFor(model => model.FacultyId)
</th>
<th>
@Html.DisplayNameFor(model => model.SemesterId)
</th>
<th>
@Html.DisplayNameFor(model => model.MajorId)
</th>
<th>
@Html.DisplayNameFor(model => model.DateOfExame)
</th>
<th>
@Html.DisplayNameFor(model => model.Capacity)
</th>
<th>
@Html.DisplayNameFor(model => model.locationOfExame)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TeacherId)
</td>
<td>
@Html.DisplayFor(modelItem => item.LessonId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClassId)
</td>
<td>
@Html.DisplayFor(modelItem => item.DegreeId)
</td>
<td>
@Html.DisplayFor(modelItem => item.FacultyId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SemesterId)
</td>
<td>
@Html.DisplayFor(modelItem => item.MajorId)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateOfExame)
</td>
<td>
@Html.DisplayFor(modelItem => item.Capacity)
</td>
<td>
@Html.DisplayFor(modelItem => item.locationOfExame)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
最佳答案
你的 View 可能有一个像这样的强类型模型
@model IEnumerable<DomainClasses.Schedule>
但您返回的是 List<SchedulePresentation>
.
一种解决方案是将模型行更改为:@model IEnumerable<SchedulePresentation>
.
关于c# - 在 MVC 中返回列表会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604807/