asp.net mvc - una - ActionLink con múltiples parámetros
pasar parametros por url asp.net c# (3)
Además de la respuesta de Mikael Östberg, agregue 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
});
entonces en tu control
// the name of the parameter must match the global.asax route
public action result Details(string name, int year)
{
return View();
}
Quiero crear una URL como /?name=Macbeth&year=2011
con mi ActionLink
que he intentado hacer así:
<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>
pero no funciona ¿Cómo hago esto?
Basado en la respuesta de Mikael Östberg y en caso de que la gente necesite 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" } })
La sobrecarga que está utilizando hace que el valor del year
termine en los atributos html del enlace (verifique la fuente renderizada).
La firma de sobrecarga se ve así:
MvcHtmlString HtmlHelper.ActionLink(
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
RouteValues
colocar los valores de su ruta en el diccionario RouteValues
la RouteValues
manera:
Html.ActionLink(
"View Details",
"Details",
"Performances",
new { name = item.show, year = item.year },
null
)