Để làm điều này, tôi đã viết một phần mở rộng cho HtmlHelper được gọi là "ActionLinkBack". Các phương thức soạn các liên kết hành động trở lại cùng một bộ điều khiển một hành động và hợp nhất các giá trị tuyến hiện có với bất kỳ giá trị mới nào được chỉ định.
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());
}
Trong tái sử dụng "trang hoa" của tôi xem tôi sử dụng các phần mở rộng để soạn, bên cạnh, và các liên kết trang cá nhân theo thời gian:
@Html.ActionLinkBack("Next", new { page = (int)ViewData["Page"] + 1 }, new { @class = "navigationLink" })
này cũng dường như có liên quan: http: // stackoverflow.com/questions/5060346/howto-automatically-add-a-specific-value-from-current-route-to-all-generated-lin – DenNukem