0

In the MVC 3 i want to display data from two models viz. Student and Enrollment into a single view. Student model

public class Student
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int student_id { get; set; }

        public string student_name { get; set; }
        public string father { get; set; }
    }

Enrollment model

public class Enrollment
    {
        [Key]
        public int enrollment_id { get; set; }
        public string rollno { get; set; }
        public int student_id { get; set; }
        public string registration_no { get; set; }
        public date registration_date { get; set; }
}

My ViewModel looks like this

 public class StudentEnrollmentViewModel
    {
        public Student_Info Student_Info { get; set; }
        public Enrollment_Info Enrollment_Info { get; set; }
        [Key]
        public int ID { get; set; }
}

How do I retrieve the data from the two model and assign it to a viewmodel so that I can display it in a view? I am using Repository Design Pattern.

Sameera Thilakasiri
  • 9,452
  • 10
  • 51
  • 86
Bahdeng
  • 434
  • 8
  • 17

2 Answers2

0

You can use DynamicPage, Look into following example

We need to use a Dynamic view page. (More Information) Follow following steps:

  1. Create DynamicViewPage type

    public class DynamicViewPage : ViewPage
    {
       public new dynamic Model { get; private set; }    
       protected override void SetViewData(ViewDataDictionary viewData)
        {
            base.SetViewData(viewData);
            Model =  ViewData.Model;
        }`
    }
    
  2. Your Controller will look like

    public ActionResult Account(string returnUrl)
    {
        LoginModel loginmodel = null;//Initialize Model;
        RegistrationModel registrationModel = null ;//Initialize Model;
       // Any Extra logic
        return View("Account", new
        {
            Login = loginmodel,
            Register = registrationModel
        });
    }
    
  3. your View should Inherit from

    Inherits="DynamicViewPage"
    

Now @Model.Login will give you Loginmodel

@Model.Register will give you RegisterModel

It should work as you expected.........

Developer
  • 8,390
  • 41
  • 129
  • 238
Manas
  • 2,534
  • 20
  • 21
  • Thanks for your prompt reply... Will look into it :) – Bahdeng Jan 17 '12 at 12:58
  • I don't know about dynamic page. Because I would also like to use the viewmodel for CRUD operations. – Bahdeng Jan 17 '12 at 13:03
  • [http://lostechies.com/seanbiefeld/2010/06/23/dynamic-view-page-mvc-without-a-view-model/] will give you detail about Dynamic page. You Still can use CRUD operation as i assume data from different model will have different forms and different partial views. – Manas Jan 17 '12 at 13:06
  • LINK: http://lostechies.com/seanbiefeld/2010/06/23/dynamic-view-page-mvc-without-a-view-model/ – Manas Jan 17 '12 at 13:17
0

Generally speaking, a controller would be responsible for contacting the model, storing the result set returned in a variable/array/struc that the view would consume. The model and view components/classes would be registered in the controller.

An example:

<event-handler event="display.institutions" access="public">            
    <notify listener="userInstitutionRights" method="getInstitutionsWithDataRightsNOXML" resultKey="request.institutions" />

    <view-page name="userNav" contentKey="request.userNav"/>
    <view-page name="userInstitutions" contentKey="request.pageContent"/>

    <announce event="assemblePage" />
</event-handler>

Event display.institutions is calling a model component userInstitutionRights and storing the result in a resultKey request.institutions and is including two view pages userNav, userInstitutions where the resultKey is available to each.

jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34