Для этого я написал расширение для HtmlHelper под названием «ActionLinkBack».Методы compose action связывают обратно с тем же контроллером действие и объединяют существующие значения маршрута с любыми указанными новыми.
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());
}
В моем представлении «навигатор страниц» многократного использования я использую расширения для составленияссылки на предыдущую, следующую и отдельные страницы:
@Html.ActionLinkBack("Next", new { page = (int)ViewData["Page"] + 1 }, new { @class = "navigationLink" })