Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!!

share|improve this question

2 Answers

up vote 2 down vote accepted

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
share|improve this answer
In the same controller or in the ParentsDetail controller ? – Rabbil Jan 24 at 9: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. localhost:8080/controller/action – emre nevayeshirazi Jan 24 at 9:29
ParentsDetail is the other controller name apart from student... – Rabbil Jan 24 at 9:34
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 at 9:37
And mvc routing is 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 at 9:37
show 3 more comments

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

In view:

@model IEnumerable<ParentsInformationViewModel>
share|improve this answer
No, that only I'm not able to get how and where to put what?? – Rabbil Jan 24 at 9:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.