I am new to Visual Studio and I need your help.
I'm working with .NET Core 3.1, C#, MVC and Identity, I am trying to create a view that contains columns from 3 tables.
The expected result it would be like this:
Personal info
Last name: xxx
First name: xxx
Skills
Skill ID: 1 Skill: xxx
Skill ID: 2 Skill: xxx
Skill ID: 3 Skill: xxx
Jobs
Job ID: 1 Employee: xxx Subject: xxx
Job ID: 2 Employee: xxx Subject: xxx
What can I do to have the above view?
The 3 models that I have are:
public class ApplicationUser : IdentityUser
{
public string StuLna { get; set; } // Last name
public string StuFna { get; set; } // First name
…
public virtual ICollection<TblSkill> TblSkills { get; set; }
public virtual ICollection<TblJobHistory> TblJobHistories { get; set; }
}
public class TblSkill
{
[Key]
public int SkiIde { get; set; } // Skill ID
public string SkiSki { get; set; } // Skill
…
[ForeignKey("ApplicationUser")]
public string AppUserId { get; set; } // ApplicationUser Id
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class TblJobHistory
{
[Key]
public int JobIde { get; set; } // Job ID
public string JobEmp { get; set; } // Employee
public string JobSub { get; set; } // Subject
…
[ForeignKey("ApplicationUser")]
public string AppUserId { get; set; } // ApplicationUser Id
public virtual ApplicationUser ApplicationUser { get; set; }
}
The DbContext:
public class AppDBContext : IdentityDbContext<ApplicationUser, IdentityRole, string>
{
public AppDBContext(DbContextOptions<AppDBContext> options)
: base(options)
{
}
public virtual DbSet<TblSkill> TblSkills { get; set; }
public virtual DbSet<TblJobHistory> TblJobHistories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TblSkill>()
.HasOne(a => a.ApplicationUser)
.WithMany(s => s.TblSkills)
.HasForeignKey(a => a.AppUserId);
modelBuilder.Entity<TblJobHistory>()
.HasOne(a => a.ApplicationUser)
.WithMany(j => j.TblJobHistories)
.HasForeignKey(a => a.AppUserId);
public DbSet<MyApp.ViewModels.PersonalInfoVM> PersonalInfoVM { get; set; }
}
So far I have created these:
View model:
public class PersonalInfoVM
{
[Key]
public string AppUserId { get; set; } // ApplicationUser Id
public virtual ApplicationUser ApplicationUsers { get; set; }
public virtual TblSkill TblSkills { get; set; }
public virtual TblJobHistory TblJobHistories { get; set; }
}
Controller:
public IActionResult PersonalInfoPreview()
{
var userid = _userManager.GetUserId(HttpContext.User);
List<ApplicationUser> users = _context.Users.ToList();
List<TblSkill> skills = _context.TblSkills.ToList();
List<TblJobHistory> jobHistories = _context.TblJobHistories.ToList();
var model = from u in users
join s in skills on u.Id equals s.AppUserId into table1
from s in table1.ToList()
join j in jobHistories on u.Id equals j.AppUserId into table2
from j in table2.ToList()
select new PersonalInfoVM
{
ApplicationUsers = u,
TblSkills = s,
TblJobHistories = j
};
return View(model);
}
View page:
@model IEnumerable<MyApp.ViewModels.PersonalInfoVM >
@{ViewData["Title"] = "PersonalInfoPreview";}
<table class="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.ApplicationUsers.StuLna</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>@item.TblSkills.SkiSki</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>@item.TblJobHistories.HisSta</td>
</tr>
}
</tbody>
</table>
This is the result for the current user with 2 skills and 2 jobs:
Papadopoulos
Papadopoulos
Papadopoulos
Papadopoulos
user1-skill1
user1-skill1
user1-skill2
user1-skill2
user1-employee1
user1-employee2
user1-employee1
user1-employee2
