2

私はこれを持っています:

public class PagesModel
{
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public int PagesCount { get; set; }
    public int CurrentPage { get; set; }
    public object RouteValues { get; set; }
    public object HtmlAttributes { get; set; }
}

public static MvcHtmlString RenderPages(this HtmlHelper helper, PagesModel pages, bool isNextAndPrev = false)
{
    //some code
    var lastPageSpan = new TagBuilder("span");
    var firstValueDictionary = new RouteValueDictionary(pages.RouteValues) { { "page", pages.PagesCount } };
    lastPageSpan.InnerHtml = helper.ActionLink(">>", pages.ActionName, pages.ControllerName, firstValueDictionary, pages.HtmlAttributes).ToHtmlString();
    return MvcHtmlString.Create(lastPageSpan.ToString());
}

生成されるリンクは次のようになります。<span><a href="/Forums/Thread?Count=2&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D">&gt;&gt;</a></span>

なんで?私は何を間違っていますか?を設定する前にブレークポイントを置く.innerHtmlと、私のfirstValueDictionary見た目は完全に正常であることがわかります。何が起こっている?

更新RouteValueDictionary:パラメーターを新しく作成した匿名型 ( ) に置き換えるとnew {page = 0}、すべて正常に動作します。定義済みを使用できないのはなぜRouteValueDictionaryですか?

4

1 に答える 1

4

ActionLink ヘルパーの間違ったオーバーロードを使用しています。このようにしてみてください:

lastPageSpan.InnerHtml = helper.ActionLink(
    ">>", 
    pages.ActionName, 
    pages.ControllerName, 
    firstValueDictionary, 
    new RouteValueDictionary(pages.HtmlAttributes) // <!-- HERE!
).ToHtmlString();

overload使用していたのは次のとおりです。

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    object routeValues,
    object htmlAttributes
)

そして、ここにcorrect overloadあなたが使用する必要があるものがあります:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    RouteValueDictionary routeValues,
    IDictionary<string, object> htmlAttributes
)

違いに気づきましたか?

于 2013-02-18T16:48:03.290 に答える