【发布时间】:2019-01-12 04:54:12
【问题描述】:
我有一个从服务器调用的方法,我从表中检索上下文,这一切都是正确的,但在客户端我不想使用 FK ID 显示在视图上,为此我需要包括 de FK 表以获取所有它们的值...首先我有“_context.Curso.Include(c => c.GrauAcademico);”但是在客户端我没有使用服务器端的get方法,所以我删除了它。现在我很困惑,我不知道如何包含 FK 表。
GET 服务器方法
// GET: api/Curso
[HttpGet]
public IEnumerable<Curso> GetCurso()
{
return _context.Curso;
}
GET 客户端方法
// GET: Cursos
public async Task<IActionResult> Index()
{
List<Curso> cursos = new List<Curso>();
string path = "api/Curso";
HttpResponseMessage response = await HttpRequestBuilder.WebApiClient.GetAsync(path);
//Checking the response is successful or not which is sent using HttpClient
if (response.IsSuccessStatusCode)
{
cursos = await response.Content.ReadAsAsync<List<Curso>>();
}
return View(cursos);
}
本地调试器
Index.cshtml
@model IEnumerable<ModelsLibrary.Curso>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Nome)
</th>
<th>
@Html.DisplayNameFor(model => model.GrauAcademico)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.GrauAcademico.TipoGrau)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.CursoId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.CursoId">Details</a>
|
<a asp-action="Delete" asp-route-id="@item.CursoId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
您想使用 GrauAcademico 表中的值,该表是 Curso 的 FK,但如果不从表中获取值,则无法显示它们...
谢谢:)
【问题讨论】:
标签: c# entity-framework linq model-view-controller