Usando Html.ActionLink para llamar a la acción en diferentes controladores


Estoy tratando de navegar entre controladores usando ActionLink. Contaré mi problema con un ejemplo.

Estoy en la vista de índice de Hat controller, y estoy tratando de usar el siguiente código para crear un enlace a la acción de Detalles de Product controller.

<%= Html.ActionLink("Details", "Details", "Product", new { id=item.ID }) %>

En lugar de crear un enlace a Detalles en Product controller, esto genera una acción de enlace a detalles en Hat controller y agrega un parámetro de longitud al final de la misma:

Hat/Details/9?Length=7

No puedo usar HTML.ActionLink para cambiar entre controladores debido a este problema. Apreciaré si puede indicarme lo que estoy haciendo mal. Gracias

PD: Estoy usando la configuración de ruta predeterminada que viene con MVC

routes.MapRoute("Default", "{controller}/{action}/{id}", 
                     new { controller = "Home", action = "Index", id = "" } );
 203
Author: SteveC, 2009-04-22

8 answers

Lo que quieres es esta sobrecarga:

//linkText, actionName, controllerName, routeValues, htmlAttributes
<%=Html.ActionLink("Details", "Details", 
    "Product", new {id = item.ID}, null) %>
 374
Author: Çağdaş Tekin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-22 13:37:07

Con esos parámetros estás activando la función/método sobrecargado incorrecto.

Lo que funcionó para mí:

<%= Html.ActionLink("Details", "Details", "Product", new { id=item.ID }, null) %>

Dispara HtmlHelper.ActionLink (string linkText, string ActionName, string ControllerName, object routeValues, object htmlAttributes)

Estoy usando MVC 4.

Cheerio!

 16
Author: Stephan Venter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-06 13:58:11

Si tomas el MVC Futures assembly (que recomiendo encarecidamente), puedes usar un genérico al crear el ActionLink y un lambda para construir la ruta:

<%=Html.ActionLink<Product>(c => c.Action( o.Value ), "Details" ) %>

Puede obtener la asamblea de futuros aquí: http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

 10
Author: James Avery,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-22 12:25:35

Usted está golpeando el mal la sobrecarga de ActionLink. Prueba esto en su lugar.

<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
 7
Author: Craig Stuntz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-22 12:21:14

Yo recomendaría escribir estos ayudantes usando parámetros nombrados en aras de la claridad de la siguiente manera:

@Html.ActionLink(
    linkText: "Details",
    actionName: "Details",
    controllerName: "Product",
    routeValues: new {
        id = item.ID
    },
    htmlAttributes: null
)
 7
Author: Psi-Ed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-01 13:20:22

Pruébalo está funcionando bien

  <%:Html.ActionLink("Details","Details","Product",  new {id=item.dateID },null)%>
 5
Author: sandy barasker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 07:50:24

Una solución alternativa sería usar el objeto ayudante Url para establecer el atributo href de una etiqueta <a> como:

<a href="@Url.Action("Details", "Product",new { id=item.ID }) )">Details</a>
 3
Author: GeorgeChond,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-26 03:57:33

Este código funcionó para mí en vista parcial:

<a href="/Content/[email protected]">@item.Title</a>
 0
Author: Amin Saadati,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-15 09:22:34