显示在使用视图模型在剃刀MVC3单一视图多个模型(在视图中仅详细)(Show multiple mo

2019-07-18 15:40发布

我的任务是显示多个models为单一view.I've创建一个ViewModel为我的要求,但我不能满足我的要求。 请看看到下面的代码并纠正我在哪里MI走错了???

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

//模型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; }
    }

//视图模型我已经创建

 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;

        }

//最后这在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);
        }

我知道,上面的方法ViewModel是错误的,但如何使用它,或者我要去哪里错了,我不能让。 我想要显示的ViewModel视图的详细视图。 请纠正我,因为我在MVC3首发。

提前致谢!!

Answer 1:

在您的控制器,如下定义你的操作方法。

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

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

@model MySolution.ViewModels.ParentsInformationViewModel


Answer 2:

有没有在你看来声明您收到类型的模型

鉴于:

@model IEnumerable<ParentsInformationViewModel>


文章来源: Show multiple models in a single view using ViewModel in Razor MVC3 (with only Details in view)