如何在视图模型Linq - ASP.NET-MVC5中在List中添加多个记录以及其他模型

时间:2015-07-16 15:44:35

标签: c# linq asp.net-mvc-5 entity-framework-6 asp.net-mvc-viewmodel

我正在研究ASP.NET-MVC5应用程序。我需要从控制器到视图的多个类传递数据模型,所以我决定使用ViewModel并使用linq相应地为视图模型赋值。现在我是我的模型,学生可以有多个紧急联系,所以我使用List但在LINQ查询中得到错误。

enter image description here

视图模型

public class StudentDetailedProfileViewModel
{
    public StudentDetailedProfileViewModel() { }

    public Student _studentModel { get; set; }
    public Course _courseModel { get; set; }
    public School _schoolModel { get; set; }
    public Campus _campusModel { get; set; }
    public ContactDetail _contactDetailModel { get; set; }
    public List<EmergencyContact> _emergencyContactModel { get; set; }

}

需要返回强类型绑定数据的函数

 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; }
    }

2 个答案:

答案 0 :(得分:1)

我设法做了以下但我不确定这是否是最好的做法!!

    public StudentDetailedProfileViewModel GetStudentDetailedProfileByStudentID(int _studentID)
    {
        try
        {
             using (var _uow = new StudentProfile_UnitOfWork())
            {
                 StudentDetailedProfileViewModel StudentProfileObject = new StudentDetailedProfileViewModel();

                var _profile = (from _student in _uow.Student_Repository.GetAll()
                                join _contactDetail in _uow.ContactDetail_Repository.GetAll() on _student.StudentID equals _contactDetail.StudentID
                                join _studentCourse in _uow.Course_Repository.GetAll() on _student.StudentID equals _studentCourse.StudentID
                                join _school in _uow.School_Repository.GetAll() on _studentCourse.SchoolID equals _school.SchoolID
                                join _campus in _uow.Campus_Repository.GetAll() on _studentCourse.CampusID equals _campus.CampusID
                                where _student.StudentID == _studentID
                                select new StudentDetailedProfileViewModel { _studentModel = _student, _contactDetailModel = _contactDetail, _courseModel = _studentCourse,_schoolModel = _school, _campusModel = _campus}).FirstOrDefault();

                _profile._emergencyContactModel = (from _emergencyContact in _uow.EmergencyContact_Repository.GetAll()
                                                  where _emergencyContact.StudentID == _studentID
                                                  select _emergencyContact).ToList();


                return _profile;                
            }
        }//
        catch { return null; }

    }

答案 1 :(得分:0)

您需要确保您定义的视图模型与您从数据库中保存的值相同。

如果在视图模型中包含任何List类型视图模型,则可以使用以下代码管理返回对象

Var getData = new List < ParentViewModel > ();


getData = (from objtbl1 in ParentTable
           select new ParentViewModel
           {
            proty1 = objtbl1 .proty1,
            childViewModelProprt = (from objChildtbl1 in childTable
                                   select new ChildViewModel
                                   {
                                        childPrty1 = objChildtbl1.childPrty1
                                    }).toList()
           }).toList();

return getData;

如果有帮助,请告诉我。