2015-05-09 49 views
1

我寫了一個自定義的HtmlHelper如下所示:MVC ActionLink呈現錯誤的HTML?

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) 
{ 
    if (routeValues == null && htmlAttributes != null) 
     return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId), actionName, controllerName, htmlAttributes); 

    return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId), 
     actionName, controllerName, 
     routeValues, 
     htmlAttributes); 
} 

這是確定的,如果routeValueshtmlAttributes兩者都是空。
但如果htmlAttributes具有價值和routeValues爲空,它呈現a標籤爲如下:

<a comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" count="1" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/Home/Login?Length=7">Exit</a> 

這有什麼錯呢?

+0

我只是測試它,它不會改變。 –

+2

像往常一樣,我猜,錯誤的重載http://stackoverflow.com/questions/4357856/razor-actionlink-autogenerating-length-7-in-url –

+0

唯一重載的'ActionLink',接受一個'字符串'因爲前3個參數還需要第4個參數作爲路由值,第5個參數作爲html屬性,這意味着如果「if」塊的計算結果爲true,則將htmlAttributes作爲路由值進行重新渲染。只需刪除這個幫助程序 - 它根本不會執行內置的ActionLink()幫助程序。 –

回答

1

試試這個:

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) 
{ 
    if (routeValues == null) 
     routeValues = new RouteValueDictionary(); 

    if (htmlAttributes == null) 
      htmlAttributes = new Dictionary<string, object>(); 

    htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId), 
     actionName, controllerName, 
     routeValues, 
     htmlAttributes); 
} 
+0

我測試過了,它不會改變任何東西! –