从 ICollection 获取视图中的值以传递 ActionLink 参数



Hellow!,我需要能够在Gestion视图中从Compromisos获取Id。我的需求是让Id评估到操作链接并转到查看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})但不起作用。

有什么建议吗?

你应该迭代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>
}

@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);
}

相关内容

  • 没有找到相关文章

最新更新