我正在使用ASP.NET-MVC5应用程序。我有三个模型类,我需要传递模型从控制器到视图的数据,所以我使用view-Model类来组合所有三个类并尝试传递但是我收到错误,如屏幕截图所示。我需要方法“GetCourseByStudentID”
的帮助
public List<StudentCourseSchoolAndCampusViewModel> GetCourseByStudentID(int _studentID)
{
try
{
using (var _uow = new StudentProfile_UnitOfWork())
{
var _record = (from _course in _uow.Course_Repository.GetAll()
join _school in _uow.School_Repository.GetAll() on _course.SchoolID equals _school.SchoolID
join _campus in _uow.Campus_Repository.GetAll() on _course.CampusID equals _campus.CampusID
where _course.StudentID == _studentID
select new { _course, _school, _campus }).ToList();
return _record;
}
}
catch { return null; }
}
public class StudentCourseSchoolAndCampusViewModel
{
public StudentCourseSchoolAndCampusViewModel() { }
public Course _courseModel { get; set; }
public School _schoolModel { get; set; }
public Campus _campusModel { get; set; }
}
public ActionResult EditStudentCourse()
{
var _StudentCourseModel = _studentProfileServices.GetCourseByStudentID(6);
return null; // incomplete
}
答案 0 :(得分:2)
您可以使用Select方法生成实体类型列表:
public List<StudentCourseSchoolAndCampusViewModel> GetCourseByStudentID(int _studentID)
{
try
{
using (var _uow = new StudentProfile_UnitOfWork())
{
var _record = (from _course in _uow.Course_Repository.GetAll()
join _school in _uow.School_Repository.GetAll() on _course.SchoolID equals _school.SchoolID
join _campus in _uow.Campus_Repository.GetAll() on _course.CampusID equals _campus.CampusID
where _course.StudentID == _studentID
select new { _course, _school, _campus }).ToList();
var studentsVM = _record.Select(s=> new StudentCourseSchoolAndCampusViewModel()
{
_courseModel = s._course,
_schoolModel = s._school,
_campusModel = s._campus
}
);
return studentsVM;
}
}
catch { return null; }
}
如果你想知道Select(Func选择器)是如何工作的 - 这是MSDN上的解释: https://msdn.microsoft.com/en-us/library/vstudio/bb548891(v=vs.100).aspx
答案 1 :(得分:1)
//...
var _record = (from _course in _uow.Course_Repository.GetAll()
join _school in _uow.School_Repository.GetAll() on _course.SchoolID equals _school.SchoolID
join _campus in _uow.Campus_Repository.GetAll() on _course.CampusID equals _campus.CampusID
where _course.StudentID == _studentID
// Here is the problem, you have to specify the type.
select new StudentCourseSchoolAndCampusViewModel {
Course = _course,
School = _school,
Campus = _campus }).ToList();
我认为这将解决您的问题。
答案 2 :(得分:0)
非常感谢Andriy指导...我有点改变并且决定发送视图模型类的模型而不是列表......
public partial class StudentProfileServices
{
#region Get Student Course By StudentID
public StudentCourseSchoolAndCampusViewModel GetCourseByStudentID(int _studentID)
{
try
{
using (var _uow = new StudentProfile_UnitOfWork())
{
var _record = (from _course in _uow.Course_Repository.GetAll()
join _school in _uow.School_Repository.GetAll() on _course.SchoolID equals _school.SchoolID
join _campus in _uow.Campus_Repository.GetAll() on _course.CampusID equals _campus.CampusID
where _course.StudentID == _studentID
select new StudentCourseSchoolAndCampusViewModel {_courseModel= _course, _schoolModel = _school, _campusModel = _campus }).FirstOrDefault();
return _record;
}
}
catch { return null; }
}
public ActionResult EditStudentCourse()
{
StudentCourseSchoolAndCampusViewModel _StudentCourseModel = new StudentCourseSchoolAndCampusViewModel();
_StudentCourseModel = _studentProfileServices.GetCourseByStudentID(6);
return PartialView("EditStudentCourse_Partial", _StudentCourseModel);
}
@model App.DAL.Model.StudentCourseSchoolAndCampusViewModel
<h1>edit student course</h1>
@Model._courseModel.ModeOfStudy // to test the model data