C# 如何在ASP.NET Core 3.1中显示连接模型的属性值
在公司视图列表中显示用户名(创建公司的人)时出现问题 这里是“User.cs”模型的一部分:C# 如何在ASP.NET Core 3.1中显示连接模型的属性值,c#,entity-framework-core,asp.net-core-3.1,C#,Entity Framework Core,Asp.net Core 3.1,在公司视图列表中显示用户名(创建公司的人)时出现问题 这里是“User.cs”模型的一部分: using System; using System.Collections.Generic; public partial class User { public User() { // init here } // Connections with other Models are here as ICollection public in
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{
// init here
}
// Connections with other Models are here as ICollection
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
}
using System;
using System.Collections.Generic;
public partial class Company
{
public Company()
{
Accounts = new HashSet<Accounts>();
CompanyHistory= new HashSet<CompanyHistory>();
Earnings= new HashSet<Earnings>();
Expences= new HashSet<Expences>();
}
// other properties those aren't important
public int? UserID { get; set; } // here is User ID
// Other connections those aren't important
public virtual User UserNavigation { get; set; } // Here is connection with Users
}
public class CompanyController : Controller
{
private readonly MyDBContext _dbContext;
public PoslodavacController(MyDBContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult ViewCompanies()
{
return View(_dbContext.Company);
}
// other code irrelevant for this problem/solution
}
@model IEnumerable<MyDatabase.Models.Company>
@using MyDatabase.Models;
@{
ViewData["Title"] = "ViewCompanies";
Layout = "_Layout";
}
<h1>ViewCompanies</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<!-- other html -->
<th>
<p>User Name</p>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<!-- Other html data -->
<td>
<!-- ATENTION HERE: @item.KorisnikNavigation is always null so the Name is not retrieved-->
<a asp-route-id="@item.User" asp-controller="User" asp-action="Details">@item.UserNavigation?.Name</a>
</td>
</tr>
}
</tbody>
</table>
这是模型“Company.cs”:
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{
// init here
}
// Connections with other Models are here as ICollection
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
}
using System;
using System.Collections.Generic;
public partial class Company
{
public Company()
{
Accounts = new HashSet<Accounts>();
CompanyHistory= new HashSet<CompanyHistory>();
Earnings= new HashSet<Earnings>();
Expences= new HashSet<Expences>();
}
// other properties those aren't important
public int? UserID { get; set; } // here is User ID
// Other connections those aren't important
public virtual User UserNavigation { get; set; } // Here is connection with Users
}
public class CompanyController : Controller
{
private readonly MyDBContext _dbContext;
public PoslodavacController(MyDBContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult ViewCompanies()
{
return View(_dbContext.Company);
}
// other code irrelevant for this problem/solution
}
@model IEnumerable<MyDatabase.Models.Company>
@using MyDatabase.Models;
@{
ViewData["Title"] = "ViewCompanies";
Layout = "_Layout";
}
<h1>ViewCompanies</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<!-- other html -->
<th>
<p>User Name</p>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<!-- Other html data -->
<td>
<!-- ATENTION HERE: @item.KorisnikNavigation is always null so the Name is not retrieved-->
<a asp-route-id="@item.User" asp-controller="User" asp-action="Details">@item.UserNavigation?.Name</a>
</td>
</tr>
}
</tbody>
</table>
这里是查看“viewcompanys.cshtml”:
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{
// init here
}
// Connections with other Models are here as ICollection
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
}
using System;
using System.Collections.Generic;
public partial class Company
{
public Company()
{
Accounts = new HashSet<Accounts>();
CompanyHistory= new HashSet<CompanyHistory>();
Earnings= new HashSet<Earnings>();
Expences= new HashSet<Expences>();
}
// other properties those aren't important
public int? UserID { get; set; } // here is User ID
// Other connections those aren't important
public virtual User UserNavigation { get; set; } // Here is connection with Users
}
public class CompanyController : Controller
{
private readonly MyDBContext _dbContext;
public PoslodavacController(MyDBContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult ViewCompanies()
{
return View(_dbContext.Company);
}
// other code irrelevant for this problem/solution
}
@model IEnumerable<MyDatabase.Models.Company>
@using MyDatabase.Models;
@{
ViewData["Title"] = "ViewCompanies";
Layout = "_Layout";
}
<h1>ViewCompanies</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<!-- other html -->
<th>
<p>User Name</p>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<!-- Other html data -->
<td>
<!-- ATENTION HERE: @item.KorisnikNavigation is always null so the Name is not retrieved-->
<a asp-route-id="@item.User" asp-controller="User" asp-action="Details">@item.UserNavigation?.Name</a>
</td>
</tr>
}
</tbody>
</table>
@model IEnumerable
@使用MyDatabase.Models;
@{
ViewData[“Title”]=“ViewCompanys”;
布局=“_布局”;
}
视图公司
创造新的
用户名
@foreach(模型中的var项目)
{
@item.UserNavigation?.Name
}
我不知道为什么@item.KorisnikNavigation为空。
我是否遗漏了公司构造函数中的一些代码?如果是,所需的代码是什么
谢谢 修复操作
public IActionResult ViewCompanies()
{
var model=_dbContext.Set<Company>().Include(i=> i.UserNavigation ).ToArray();
return View(model);
}
public IActionResult ViewCompanies()
{
var model=_dbContext.Set().Include(i=>i.UserNavigation.ToArray();
返回视图(模型);
}