我有这个:
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&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D">>></a></span>
.innerHtml
之前放置断点时,我看到我的firstValueDictionary
看起来完全正常。这是怎么回事?
更新:当我替换RouteValueDictionary
时参数使用新创建的匿名类型( new {page = 0}
),那么一切正常。为什么我不能使用预定义的 RouteValueDictionary
?
最佳答案
您使用了 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
)
注意到区别了吗?
关于asp.net - Html.ActionLink 生成的奇怪链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14940932/