【问题标题】:How to add multiple records in List along with other model in view-model Linq - ASP.NET-MVC5如何在 List 中添加多个记录以及视图模型 Linq 中的其他模型 - ASP.NET-MVC5
【发布时间】:2015-07-16 15:44:35
【问题描述】:

我正在开发 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; }

}

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

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

【问题讨论】:

  • 图片和代码不同。哪个有问题?
  • 图片是我遇到问题的方法的屏幕截图!!!
  • 对于那个特定的 StudentId,有多少紧急联系人? _uow.EmergencyContact_Repository.GetAll() 似乎返回了一个 EmergencyContact,而不是预期的 List()。
  • 总是两个...这就是为什么在我的模型中我声明为强类型列表
  • FWIW,从这样的方法中访问您的上下文是一个非常糟糕的主意。通常,您希望避免 using 使用 EF 上下文,或者您必须非常小心以确保您急切地加载每个可能被访问的关系。此外,当您有多个上下文实例浮动时,您会遇到各种问题。理想情况下,每个请求只需要一个实例,并且应该使用某种依赖注入来将其提供给需要它的任何类/控制器。

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


【解决方案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; }

    }

【讨论】:

    【解决方案2】:

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

    如果您在视图模型中持有任何列表类型的视图模型,则可以使用以下代码管理返回对象

    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;
    

    如果有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2020-02-03
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多