Per fare ciò, ho scritto un'estensione di HtmlHelper chiamata "ActionLinkBack". I metodi compongono i link di azione sullo stesso controller un'azione e uniscono i valori di route esistenti con quelli nuovi specificati.
public static HtmlString ActionLinkBack(this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, object routeValues)
{
return ActionLinkBack(htmlHelper, linkText, new RouteValueDictionary(routeValues), new RouteValueDictionary());
}
public static HtmlString ActionLinkBack(this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, object routeValues, object htmlAttributes)
{
return ActionLinkBack(htmlHelper, linkText, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));
}
public static HtmlString ActionLinkBack(this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, RouteValueDictionary routeValues)
{
return ActionLinkBack(htmlHelper, linkText, routeValues, new RouteValueDictionary());
}
public static HtmlString ActionLinkBack(this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
// Build a new dictionary of route values based on the previous set
var newRouteValues = new RouteValueDictionary(htmlHelper.ViewContext.RouteData.Values);
// Retain current querystring parameters
var queryString = htmlHelper.ViewContext.HttpContext.Request.QueryString;
if (queryString.Count > 0)
{
foreach (string key in queryString.Keys)
{
newRouteValues[key] = queryString[key];
}
}
// Add and override entries from the list of new route values
if (routeValues != null)
{
foreach (var routeValueItem in routeValues)
{
newRouteValues[routeValueItem.Key] = routeValueItem.Value;
}
}
return new HtmlString(htmlHelper.ActionLink(linkText, null, newRouteValues, htmlAttributes).ToHtmlString());
}
Nel mio riutilizzabile "pagina navigatore" vista che utilizzare le estensioni per comporre i precedenti, successivi e singoli collegamenti della pagina:
@Html.ActionLinkBack("Next", new { page = (int)ViewData["Page"] + 1 }, new { @class = "navigationLink" })
Questo sembra anche rilevante: http: // stackoverflow.com/questions/5060346/howto-automatically-add-a-specific-value-from-current-route-to-all-generated-lin – DenNukem