Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC ActionLink呈现错误的html?_C#_Model View Controller_Html Helper_Actionlink - Fatal编程技术网

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()
帮助程序尚未执行的任何操作。