2015-05-09 3 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); 
} 

Это нормально, если routeValues и htmlAttributes оба нуль.
Но если 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', которая принимает' string' так как первые 3 параметра также требуют 4-го параметра в качестве значений маршрута и 5-го параметра в качестве атрибутов html, что означает, что если ваш блок 'if' оценивается как истинный, ваше разграничение 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

Я протестировал, ничего не меняет !!! –