如何在 List 中添加多条记录以及视图模型 Linq 中的其他模型 - ASP.NET-MVC5
How to add multiple records in List along with other model in view-model Linq - ASP.NET-MVC5
我正在开发 ASP.NET-MVC5 应用程序。我需要将来自多个 类 的数据模型从控制器传递到视图,所以我决定使用 ViewModel 并使用 linq 相应地将值分配给视图模型。现在我是我的模型,Student 可以有多个紧急联系人,所以我正在使用 List 但在 LINQ 查询中这部分出现错误。
视图模型
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; }
}
需要return强类型绑定数据的函数
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 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; }
}
您需要确保您定义的视图模型与您从数据库中保存的值相同。
如果您在视图模型中持有任何列表类型的视图模型,您可以使用以下代码管理 return 对象
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;
如果有帮助请告诉我。
我正在开发 ASP.NET-MVC5 应用程序。我需要将来自多个 类 的数据模型从控制器传递到视图,所以我决定使用 ViewModel 并使用 linq 相应地将值分配给视图模型。现在我是我的模型,Student 可以有多个紧急联系人,所以我正在使用 List 但在 LINQ 查询中这部分出现错误。
视图模型
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; }
}
需要return强类型绑定数据的函数
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 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; }
}
您需要确保您定义的视图模型与您从数据库中保存的值相同。
如果您在视图模型中持有任何列表类型的视图模型,您可以使用以下代码管理 return 对象
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;
如果有帮助请告诉我。