【发布时间】:2020-07-27 07:40:36
【问题描述】:
我是 Visual Studio 新手,需要您的帮助。
我正在使用 .NET Core 3.1、C#、MVC 和 Identity,我正在尝试创建一个包含 3 个表中的列的视图。
预期的结果应该是这样的:
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
我该怎么做才能获得上述视图?
我拥有的 3 个模型是:
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; }
}
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; }
}
到目前为止,我已经创建了这些:
查看模型:
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; }
}
控制器:
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);
}
查看页面:
@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>
这是当前用户拥有 2 个技能和 2 个工作的结果:
Papadopoulos
Papadopoulos
Papadopoulos
Papadopoulos
user1-skill1
user1-skill1
user1-skill2
user1-skill2
user1-employee1
user1-employee2
user1-employee1
user1-employee2
【问题讨论】:
-
能否请您也提供预期结果?您提供了很多代码 - 谢谢 - 但缺少实际问题或问题描述。
-
我更正并将预期结果放在问题的顶部
-
请不要使用匈牙利符号。删除
Tbl前缀。 -
另外,格式化和缩进您的代码,以便我们阅读。并且不要将实体类型用作视图模型。
-
谢谢戴。我格式化并缩进了代码。您能更具体地了解视图模型吗?
标签: .net-core asp.net-core-mvc asp.net-identity