C# MVC ActionLink呈现错误的html?
我编写了一个定制的C# MVC ActionLink呈现错误的html?,c#,model-view-controller,html-helper,actionlink,C#,Model View Controller,Html Helper,Actionlink,我编写了一个定制的HtmlHelper,如下所示: public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) { if (routeValues == null &&a
HtmlHelper
,如下所示:
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>
有什么问题吗?试试这个:
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);
}
public static MvcHtmlString MdActionLink(此HtmlHelper HtmlHelper、string resourceId、string actionName、string controllerName、object RouteValue=null、object htmlAttributes=null)
{
if(routeValues==null)
routeValues=新的RouteValueDictionary();
如果(htmlAttributes==null)
htmlAttributes=新字典();
htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId),
actionName,controllerName,
路线价值,
(三),;
}
我刚刚测试了它,它没有改变。和往常一样,我猜,错误重载是ActionLink
唯一的重载,它接受字符串作为前3个参数,还需要第4个参数作为路由值,第5个参数作为html属性,这意味着如果if
块的计算结果为true,将HtmLatAttributes呈现为路由值。只需删除此帮助程序-它不会执行内置ActionLink()
帮助程序尚未执行的任何操作。