2011-04-08 20 views
26

Quiero crear una URL como /?name=Macbeth&year=2011 con mi ActionLink que he intentado hacer de este modo:ActionLink con múltiples parámetros

<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%> 

pero no funciona. ¿Cómo hago esto?

Respuesta

56

La sobrecarga que está utilizando hace que el valor year termine en los atributos html del enlace (consulte la fuente representada).

La firma de sobrecarga tiene el siguiente aspecto:

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes 
) 

Es necesario poner tanto sus valores de ruta en el diccionario RouteValues así:

Html.ActionLink(
    "View Details", 
    "Details", 
    "Performances", 
    new { name = item.show, year = item.year }, 
    null 
) 
+4

¿Y cómo generar una ruta como '/ Macbeth/2011'? – bjan

7

Además de respuesta Mikael Östberg añadir algo como esto en su global.asax

routes.MapRoute(
    "View Details", 
    "Performances/Details/{name}/{year}", 
    new { 
     controller ="Performances", 
     action="Details", 
     name=UrlParameter.Optional, 
     year=UrlParameter.Optional 
    }); 

y luego en su cont rodillo

// the name of the parameter must match the global.asax route  
public action result Details(string name, int year) 
{ 
    return View(); 
} 
2

Basado en Mikael Östberg responda y por si la gente necesita saber cómo funciona con html attr. Aquí hay otro ejemplo, referencia de ActionLink

@Html.ActionLink("View Details", 
"Details", 
"Performances", 
    new { name = item.show, year = item.year }, 
    new {@class="ui-btn-right", data_icon="gear"}) 


@Html.ActionLink("View Details", 
"Details", 
"Performances", new RouteValueDictionary(new {id = 1}),new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } }) 
Cuestiones relacionadas