4

ActionResultを返す拡張メソッドがあります(デモンストレーション用に簡略化されています)。

public static ActionResult GetTestActionResult(this HtmlHelper htmlHelper, int productId)
{
    return MVC.Products.Details(productId);
}

私はこれをHtml.ActionLinkで使用しています:

@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" });

ナビゲーションにこれらのハッシュフラグメントを使用する、タブ用のカスタムjQueryプラグインを使用しています。URLの末尾にハッシュフラグメントをタグ付けして、開きたいタブを追加したいと思います。

Html.ActionLinkには、フラグメントのオーバーロードがあります。つまり、次のようになります。

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    string protocol,
    string hostName,
    string fragment,
    Object routeValues,
    Object htmlAttributes
)

しかし、それはT4MVCが削除するように設計されている厄介なマジックストリングでいっぱいです。静的拡張メソッド(GetTestActionResult)のルートディクショナリにフラグメントを追加する方法はありますか?

何かのようなもの:

return MVC.Products.Details(productId).AddRouteValue(String.Empty, "#tab-similar-products");

SOには2つの類似した質問と回答があることを認識していますが、それらは私が探しているものを完全に提供してくれません。ビューに戻す前に、フラグメントをActionResultにラップする必要があります。

  1. ASP.NETMVCURLルートにハッシュ値を含める
  2. URLフラグメントを使用してT4MVCActionLinkを作成します

アップデート:

以下のDavidEbboの修正を使用して、次の変更を行いました。少しハッキーですが、機能します:

最初に、ActionResultを返す内部関数を変更して、フラグメントをルート値としても追加するようにしました(理想的ではありませんが機能します)。

return MVC.Products.Details(productId).AddRouteValue("tab", "#tab-similar-products");

次に、ビューでそのフラグメント値をルートディクショナリからコピーし、完全を期すためにそのルート値を削除します。

// get my ActionResult with the tab fragment tagged on as a route value
var actionResult = Html.GetTestActionResult(item.Key, Model.ClaimId);

// get the tab fragment value
var tabRoute = actionResult.GetRouteValueDictionary().FirstOrDefault(r => r.Key == "tab").Value ?? "none";

// remove the route value, otherwise it will get tagged to the querystring
actionResult.GetRouteValueDictionary().Remove("tab");

// display
@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: tabRoute.ToString());

ActionResultを使用してフラグメントを返すためのよりきれいな方法があると確信していますが、今のところこれは機能します。デビッドに感謝します。

4

1 に答える 1

6

T4MVCは、これを処理するために新しいオーバーロードを必要とします。T4MVC.ttで、次を変更してみてください。

public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes) {
  return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes));
}

public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, IDictionary<string, object> htmlAttributes) {
  return htmlHelper.RouteLink(linkText, result.GetRouteValueDictionary(), htmlAttributes);
}

public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
  return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes), protocol, hostName, fragment);
}

public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, IDictionary<string, object> htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
  return htmlHelper.RouteLink(linkText, null, protocol, hostName, fragment, result.GetRouteValueDictionary(), htmlAttributes);
}

そうすれば、次のようなものを書くことができます。

@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: "#tab-similar-products")

それが機能するかどうか教えてください。メインテンプレートに追加してみます。

于 2011-06-10T06:25:36.183 に答える