.net core 创建包含3个表中的列的视图

.net core 创建包含3个表中的列的视图,.net-core,asp.net-core-mvc,asp.net-identity,.net Core,Asp.net Core Mvc,Asp.net Identity,我是Visual Studio新手,需要您的帮助 我正在使用.NETCore3.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

我是Visual Studio新手,需要您的帮助

我正在使用.NETCore3.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; }
}
到目前为止,我已经创建了这些:

视图模型:

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);
}
    public IActionResult Details()
    {
        //query the application user table and find the related entities.
        var result = _context.Users
            .Include(c=>c.TblSkills)
            .Include(c=>c.TblJobHistories)
            .Where(c => c.UserName == "Dillion")
            .Select(c=> new PersonalInfoVM() { AppUserId = c.Id, ApplicationUsers = c }).FirstOrDefault();
        return View(result);
    }

代码应用程序中有几个问题超出了问题的范围

首先要做的是更改视图模型:

public class ViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<Skills> Skills { get; set; }
    public List<Jobs> Jobs { get; set; }

}
这将在问题开始时输出结果作为示例。在您看来,您似乎希望返回所有用户,然后只需删除where子句并将
FirstOrDefault
更改为
ToList()


请注意,您的视图必须反映更新的ViewModel

我将您的ViewModel设置如下:

public class PersonalInfoVM
{
   public string  AppUserId { get; set; } // ApplicationUser Id
   public ApplicationUser ApplicationUser { get; set; }
   public IEnumerable<TblSkill> TblSkills { get; set; }
   public IEnumerable<TblJobHistory> TblJobHistories { get; set; }
}
最后像这样展示:

@model  MyApp.ViewModels.PersonalInfoVM;
@{ViewData["Title"] = "PersonalInfoPreview";}

<table class="table">
    <tbody> 
            <tr>
                <td colspan="3">Personal info</td>
            </tr>
            <tr>
                <td colspan="2">First name</td>
                <td>@Model.ApplicationUser.FirstName</td>
            </tr>
         
            <tr>
                <td colspan="2">Last name</td>
                <td>@Model.ApplicationUser.LastName</td>
            </tr>
         

        @foreach (var item in Model.TblSkills)
        {
            <tr>
                <td colspan="2">@item.SkiIde</td>
                <td>@item.SkiSki</td>
            </tr>
        } 
        @foreach (var item in Model.TblJobHistories )
        {
            <tr>
                <td>@item.JobIde </td>
                <td>@item.JobEmp </td>
                <td>@item.JobSub </td>
            </tr>
        }

    </tbody>
</table>
@model MyApp.ViewModels.PersonalInfoVM;
@{ViewData[“Title”]=“PersonalInfoPreview”;}
个人信息
名字
@Model.ApplicationUser.FirstName
姓
@Model.ApplicationUser.LastName
@foreach(Model.TblSkills中的var项)
{
@项目1.skide
@斯基斯基
} 
@foreach(Model.TblJobHistories中的var项)
{
@item.JobIde
@item.JobEmp
@项目.JobSub
}

根据您的代码,我假设您希望查询特定的用户信息,并获取相关的技能和工作历史记录。如果是这种情况,因为您已经为这些表配置了一对多关系,我认为您可以使用Include方法来查找相关的实体。请参考以下示例代码:

DBcontext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Projects> Projects { get; set; }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<TestTable> TestTables { get; set; }



    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<Blog>()
            .HasMany(b => b.Posts)
            .WithOne();
    }
}
视图模型:

public class PersonalInfoVM
{
    [Key]
    public string AppUserId { get; set; } // ApplicationUser Id
    public virtual ApplicationUser ApplicationUsers { get; set; }
}
查看页面中的代码:

@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>
@model EFCore.Models.PersonalInfoVM

@{
    ViewData["Title"] = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Details</h1>

<div>
    <h4>PersonalInfoVM</h4>
    <hr />
    <div>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.AppUserId)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.AppUserId)
            </dd>
        </dl>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.ApplicationUsers.UserName)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.ApplicationUsers.UserName)
            </dd>
        </dl>
        @foreach (var item in Model.ApplicationUsers.TblSkills)
        {
            <dl class="row">
                <dt class="col-sm-2">
                    SkiSki
                </dt>
                <dd class="col-sm-10">
                    @item.SkiSki
                </dd>
            </dl>
        }
        @foreach (var item in Model.ApplicationUsers.TblJobHistories)
        {
            <dl class="row">
                <dt class="col-sm-2">
                    Employee:
                </dt>
                <dd class="col-sm-10">
                    @item.JobEmp
                </dd>
            </dl>
            <dl class="row">
                <dt class="col-sm-2">
                    Subject:
                </dt>
                <dd class="col-sm-10">
                    @item.JobSub
                </dd>
            </dl>
        }
    </div>
</div>
<div>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    <a asp-action="Index">Back to List</a>
</div>
@model EFCore.Models.PersonalInfoVM
@{
ViewData[“标题”]=“详细信息”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
细节
个人信息虚拟机

@DisplayNameFor(model=>model.AppUserId) @DisplayFor(model=>model.AppUserId) @DisplayNameFor(model=>model.ApplicationUsers.UserName) @DisplayFor(model=>model.ApplicationUsers.UserName) @foreach(Model.ApplicationUsers.TblSkills中的变量项) { 斯基斯基 @斯基斯基 } @foreach(Model.ApplicationUsers.TblJobHistories中的var项) { 雇员: @item.JobEmp 主题: @项目.JobSub } @ActionLink(“编辑”,“编辑”,新的{/*id=Model.PrimaryKey*/})|


有关阅读相关数据的更多详细信息,您可以查看。

您能否提供预期结果?您已经提供了很多代码-谢谢-但实际问题或问题描述缺失。我已更正,并将预期结果放在问题顶部。请不要使用匈牙利符号。删除
Tbl
前缀。另外,格式化并缩进代码,以便我们阅读。不要将实体类型用作视图模型。谢谢。我格式化并缩进了代码。您能更具体地描述视图模型吗?我只需要特定用户的值
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Projects> Projects { get; set; }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<TestTable> TestTables { get; set; }



    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<Blog>()
            .HasMany(b => b.Posts)
            .WithOne();
    }
}
    public IActionResult Details()
    {
        //query the application user table and find the related entities.
        var result = _context.Users
            .Include(c=>c.TblSkills)
            .Include(c=>c.TblJobHistories)
            .Where(c => c.UserName == "Dillion")
            .Select(c=> new PersonalInfoVM() { AppUserId = c.Id, ApplicationUsers = c }).FirstOrDefault();
        return View(result);
    }
public class PersonalInfoVM
{
    [Key]
    public string AppUserId { get; set; } // ApplicationUser Id
    public virtual ApplicationUser ApplicationUsers { get; set; }
}
@model EFCore.Models.PersonalInfoVM

@{
    ViewData["Title"] = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Details</h1>

<div>
    <h4>PersonalInfoVM</h4>
    <hr />
    <div>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.AppUserId)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.AppUserId)
            </dd>
        </dl>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.ApplicationUsers.UserName)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.ApplicationUsers.UserName)
            </dd>
        </dl>
        @foreach (var item in Model.ApplicationUsers.TblSkills)
        {
            <dl class="row">
                <dt class="col-sm-2">
                    SkiSki
                </dt>
                <dd class="col-sm-10">
                    @item.SkiSki
                </dd>
            </dl>
        }
        @foreach (var item in Model.ApplicationUsers.TblJobHistories)
        {
            <dl class="row">
                <dt class="col-sm-2">
                    Employee:
                </dt>
                <dd class="col-sm-10">
                    @item.JobEmp
                </dd>
            </dl>
            <dl class="row">
                <dt class="col-sm-2">
                    Subject:
                </dt>
                <dd class="col-sm-10">
                    @item.JobSub
                </dd>
            </dl>
        }
    </div>
</div>
<div>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    <a asp-action="Index">Back to List</a>
</div>