4

My task is to show multiple models into a single view.I've created a ViewModel for my requirement but I'm not meeting my requirement. please have a look into the below code and rectify me where m i going wrong ???

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

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

//ViewModel Which I have created

 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;

        }

//And finally this is my method defined in the StudentController (Have i defined it in a right place/way??)

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

I know that the above method of a ViewModel is wrong but how to use it or where am i going wrong I can't get. I want to display the ViewModel in the view as a detailed view . Please, correct me as I'm a starter in MVC3 .

Thanks In Advance!!

MatthiasG
  • 4,434
  • 3
  • 27
  • 47
Rabbil
  • 43
  • 1
  • 8

2 Answers2

6

In your controller, define your action method as follows.

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

In your view define your model.

@model MySolution.ViewModels.ParentsInformationViewModel
emre nevayeshirazi
  • 18,983
  • 12
  • 64
  • 81
  • In the same controller or in the ParentsDetail controller ? – Rabbil Jan 24 '13 at 09:09
  • ParentsDetails is a weird controller name. Are you sure it is not Parents ? and your action name is ParentsDetail ? It should be sth like following in default MVC routing. http://localhost:8080/controller/action – emre nevayeshirazi Jan 24 '13 at 09:29
  • Ok. then place this method there. It is totally up to you. If you want to view this page when user requests /ParentsDetails/ActionName, then put it inside that controller. – emre nevayeshirazi Jan 24 '13 at 09:37
  • And mvc routing is http://localhost:8080/ParentsDetail/ParentsDetail . just now i checked in the view but I'm not getting the data as I think I'm not passing the required list.I checked with your given code as shown in rounting... but correct me. – Rabbil Jan 24 '13 at 09:37
  • are you filling the lists properly ? and can you post your view code please. – emre nevayeshirazi Jan 24 '13 at 09:38
  • No I've not yet filled the list...I was just checking with your given code. and in my view model I've defined what you have said in above comments.Also how should i bind my list as i was trying with my list(what I've shown in my question) I was not getting the desired output . please help. – Rabbil Jan 24 '13 at 09:42
  • Thanks dear..!! I filled the list as explained by you, according to my requirement of using different models.Hence, I will display it accordingly.Thanks for your effort !! Hope to get help from this forum in future,as this was my 1st post and got a quick response. – Rabbil Jan 24 '13 at 10:29
0

Is there in your view declared that you are receiving model of type

In view:

@model IEnumerable<ParentsInformationViewModel>
ncn corp
  • 113
  • 5