Reputation: 1
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….
Upvotes: 0
Views: 1243
Reputation: 1762
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
}
Upvotes: 1
Reputation: 4918
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
Upvotes: 0