How to add multiple records in List along with other model in view-model Linq - ASP.NET-MVC5

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

Question

I am working on ASP.NET-MVC5 applications. I need to pass data model from multiple classes from controller to view so I decided it use ViewModel and use linq to assign values to the view model accordingly. Now I my model, Student can have multiple emergency contact so I am using List but getting error on this part in LINQ query.

enter image description here

ViewModel

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

}

Function that need to return strongly typed binded data

 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; }
    }
1
0
7/16/2015 3:44:35 PM

Accepted Answer

I have managed to do as following but I am not sure if it is the best practice!!

    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
7/20/2015 8:23:04 AM

Popular Answer

You need to make sure the view model you defined are same as what value you are holding from the Database.

If you hold any List type view model inside a view model, you can manage the return object with the below code

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;

Let me know if that helps.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow