How to pass parameter to Index from another Controller

Asked

Viewed 352 times

1

I have to pass a parameter (ID) to a controller’s index,

I tried to use the ActionLink but it didn’t work.

View:

<body>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Tag)
            </th> 
            <th>
                @Html.DisplayNameFor(model => model.Fluido)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Vedacao)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Criticidade)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Mtbf)
            </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <th>
            @Html.DisplayFor(modelItem => item.Tag)
        </th>
        <td>
            @Html.DisplayFor(modelItem => item.Fluido)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Vedacao)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Criticidade)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mtbf)
        </td>
        <td>       
            @Html.ActionLink("Detalhes", "Index", "RelatorioRa", new { tagId = item.TagID })
        </td>
    </tr>
}

</table>

Controler RelatorioRa who will receive the ID:

 public async Task<ActionResult> Index(int? tagId)
    {
        var relatorioRaModels = db.RelatorioRaModels.Include(r => r.RelatorioTag);

        if (tagId.HasValue)
        {
            //realiza o filtro para a tag selecionada
            relatorioRaModels = relatorioRaModels.Where(a => a.TagID == tagId);
        }

        return View(await relatorioRaModels.ToListAsync());
    }
  • Friend, the example I answered in the other question shows how to do this is the @Html.ActionLink(linkText: item.Tag, actionName: "Index", controllerName: "RelatoriosRas", routeValues: new { tagId = item.TagID }, htmlAttributes: new { })

1 answer

1


In your situation, just add one more parameter to your ActionLink which would refer to htmlAttributes

@Html.ActionLink("Detalhes", "Index", "RelatorioRa", new { tagId = item.TagID }, null)

If you pass only three parameters, he understands that his new { tagId = item.TagID } is the htmlAttributes and that the RelatorioRa is the routeValues.

  • Friend can explain to me why to use like this: @Html.ActionLink("Detalhes", "Index", "RelatorioRa", new { tagId = item.TagID }) doesn’t work? and the way you went works?

  • It’s because of the signature of ActionLink https://msdn.microsoft.com/pt-br/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118). aspx

  • Man, I owe you a beer, thanks in thanks

  • let’s drink then

Browser other questions tagged

You are not signed in. Login or sign up in order to post.