0

I have two tables StudentPersonalInformation and EducationQualification

Here in MVC, I have create two model classes StudentPersonalInformation.cs and EducationQualification.cs, here I create the object of both classes in one wrapper class and that class Name is StudentInfo.cs

public class StudentPersonalInfo {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string StudentFirstName { get; set; }
    public string StudentLastName { get; set; }
    public int Age { get; set; }
    public string BloodGroup { get; set; }
    public string Address { get; set; }

}

public class EducationQualification {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Graduation { get; set; }
    public int Grad_Marks_obtain { get; set; }

    public string Grad_passing_year { get; set; }
    public stringPost Graduation { get; set; }

    public int PG_Marks_obtain { get; set; }
    public string PG_passing_year { get; set; }    
}

public class StudentInfo    
{
    public StudentPersonalInfo PersonalInfo { get; set; }
    public EducationQualification EducationalQualification { get; set; }
}

This is the DBContext class:

public class StudentDbContext:DbContext
{
    public DbSet<StudentPersonalInfo> StudentPersonalInfos { get; set; }
    public DbSet<EducationQualification> EducationQualifications { get; set; }
}

And My Question is:

How to display the details by id of particular students from both tables in one view. Please tell me how to do this….

9
  • if you want yo make the view as strongly typed then create the class that contain the combination of both the class properties,bind the data with the class and display the data in views. Commented Apr 3, 2014 at 8:56
  • How do you know which EducationQualification belings to which student? Commented Apr 3, 2014 at 9:05
  • The most straight forward way here would be to create a stored procedure of the student detail and use Linq to get the data in the class format you require. Commented Apr 3, 2014 at 9:06
  • @Gert Arnold i dont have any relationship between two tables, i wanted to dispaly it by ID. thats meanthe studentinformation with its respective education qualification Commented Apr 3, 2014 at 9:13
  • But how do you know what to display then?? What is an EducationQualification? Is it related to students anyhow? Commented Apr 3, 2014 at 9:25

2 Answers 2

1

You should make a ViewModel and in the controller action you should populate all the properties you want the view to have.

public class MyViewModelWithMultipleLists
{
       public List<StudentPersonalInfo> Students{ get; set; } 
       public List<EducationQualification> Educations{ get; set; }
       //etc
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any query for display the perticular student info by id
In order to query your context you should use LINQ. Google 'LINQ c#' you ll find a bunch of tutorials that will teach you how to query your data.
0

First you need to create a relationship between Student and Qualification by adding a navigation property to EducationQualification, assuming each qualification has 1 student.

public virtual Student Student {get; set;}

If a student can have more than one qualification, your StudentInfo model would be more likely to look like this:

    public StudentPersonalInfo PersonalInfo { get; set; }
    public List<EducationQualification> EducationalQualification { get; set; }

So then you you can query the db, and because you have created a relationship between student and qualification, your query will look something like:

var model= (from s in Student
where s.Id = id
select new StudentInfo
{
    PersonalInfo = s,
    EducationalQualification = s.EducationQualification.ToList()
}).FirstOrDefault()

You would pass the results:

return View(model);

to a strongly typed View:

    @model StudentInfo

   @Html.DisplayFor(x => x.PersonalInfo.StudentFirstName)
   ...etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.