从ICollection获取View的值以传递ActionLink参数

时间:2018-09-06 14:24:34

标签: asp.net asp.net-mvc asp.net-core

嘿!,我需要能够在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})但不起作用。

有什么建议吗?

2 个答案:

答案 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);
    }
相关问题