使用Razor MVC3中的ViewModel在单个视图中显示多个模型(仅在视图中显示详细信息)

Rab*_*bil 4 c# razor asp.net-mvc-3 asp.net-mvc-viewmodel

我的任务是在models一个视图中显示多个ViewModel.我已经为我的要求创建了一个但我不符合我的要求.请看下面的代码并纠正我哪里出错????

public partial class StudentsDetail
    {
        public int StudentID { get; set; }
        public int ParentID { get; set; }
        public string StudentName { get; set; }
        public string Gender { get; set; }
        public string FatherName { get; set; }
        public string MotherName { get; set; }
        public Nullable<System.DateTime> DateOfBirth { get; set; }

        public virtual ParentsDetail ParentsDetail { get; set; }
        public virtual SchoolDetail SchoolDetail { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

//模型2

 public partial class ParentsDetail
    {
        public ParentsDetail()
        {
            this.StudentsDetails = new HashSet<StudentsDetail>();
        }

        public int ParentID { get; set; }
        public string Occupation { get; set; }
        public string Organization { get; set; }
        public string AnnualIncome { get; set; }

        public virtual ICollection<StudentsDetail> StudentsDetails { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

// ViewModel我创建的

 public class ParentsInformationViewModel
    {
        public List<StudentsDetail> StudentsDetails { get; set; }
        public List<ParentsDetail> ParentsDetails { get; set; }

        public ParentsInformationViewModel(List<StudentsDetail> _studentDetails, List<ParentsDetail> _parentsDetails) //Should i pass all the required parameters that i want to display in view ????
        {
            StudentsDetails = _studentDetails;
            ParentsDetails = _parentsDetails;

        }
Run Code Online (Sandbox Code Playgroud)

//最后这是我在StudentController中定义的方法(我是否在正确的位置/方式中定义了它?)

public ActionResult StudentViewModel()
        {
            ViewBag.ParentsDetail = new ParentsDetail(); //ParentsDetail is my controller
            List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails();
            List<ParentsInformationViewModel> ParentInfoVMObj = new List<ParentsInformationViewModel>();
            //foreach (var student in studentListObj)
            //{
            //    ParentInfoVMObj.Add(new ParentsInformationViewModel(student.StudentID, student.ParentID));

            //}
            //ParentInfoVMObj.Add(ParentInfoVMObj); /// don't know how to call the required viewmodel
            return View(ParentInfoVMObj);
        }
Run Code Online (Sandbox Code Playgroud)

我知道上面的方法ViewModel是错误的,但如何使用它或我在哪里出错我无法得到.我想ViewModel在视图中显示详细视图.请纠正我,因为我是MVC3的首发.

提前致谢!!

emr*_*azi 6

在控制器中,按如下方式定义操作方法.

public ActionResult ParentsDetails()
{
    var studentDetails = new List<StudentDetail>();
    var parentDetails = new List<ParentsDetail>();

    // Fill your lists here, and pass them to viewmodel constructor.
    var viewModel = new ParentsInformationViewModel(studentDetails, parentDetails)

    // Return your viewmodel to corresponding view.
    return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)

在您的视图中定义您的模型.

@model MySolution.ViewModels.ParentsInformationViewModel
Run Code Online (Sandbox Code Playgroud)