嘿!,我需要能够在Id
的视图中从Compromisos
获取Gestion
。我需要将Id
传递到ActionLink并转到Compromisos
的查看详细信息
public class Gestion
{
//abbreviated to not make the long post
public Personales Personales { get; set; }
public ICollection<Compromisos> Compromisos { get; set; }
}
和
public class Compromisos
{
//abbreviated to not make the long post
public Personales Personales { get; set; }
public Gestion Gestion { get; set; }
}
实际上我是用这个来获取Id
@foreach (var item in Model.Gestion)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Compromisos)
</td>
</tr>
}
但是我希望能够做到这一点:@Html.ActionLink("Detalle", "Details", "Compromisos", new { id = item.Compromisos})
但不起作用。
有什么建议吗?
答案 0 :(得分:0)
您应该迭代Model.Compromisos模型而不是Model.Gestion吗?也许发布整个模型。
@foreach (var item in Model.Compromisos)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Gestion.[Property])
</td>
<td>
@Html.ActionLink("Detalle", "Details", "Compromisos", new { id = item.Compromisos.Id})
</td>
</tr>
}
答案 1 :(得分:0)
@Waragi我终于做到了。
@foreach (var Item in Model.Compromisos)
{
@if (item.Id == Item.GestionId)
{
<a asp-action="Details" asp-controller="Compromisos" target="_blank" asp-route-id="@Item.Id">Detalle</a>
}
}
在控制器中,我添加了.Include(c => c.Compromisos)
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var gestion = await _context.Gestion
.Include(c => c.Compromisos) //before I had included .ThenInclude(c => c.Compromisos)
.SingleOrDefaultAsync(m => m.Id == id);
if (gestion == null)
{
return NotFound();
}
return View(gestion);
}