Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 图片按钮Ajax.ActionLink_C#_Ajax_Asp.net Mvc - Fatal编程技术网

C# 图片按钮Ajax.ActionLink

C# 图片按钮Ajax.ActionLink,c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,我正在尝试创建Ajax.ActionLink的扩展。我对Html.ActionLink及其工作原理也做了同样的处理,但当我尝试对Ajax方法进行扩展时,它不起作用。发生的情况是,当我单击链接时,它不会呈现我的局部视图(替换),而是将我重定向到该视图。我想指出的是,Ajax.ActionLink方法运行良好 这是我的密码: public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, s

我正在尝试创建
Ajax.ActionLink
的扩展。我对
Html.ActionLink
及其工作原理也做了同样的处理,但当我尝试对Ajax方法进行扩展时,它不起作用。发生的情况是,当我单击链接时,它不会呈现我的局部视图(替换),而是将我重定向到该视图。我想指出的是,Ajax.ActionLink方法运行良好

这是我的密码:

public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
    object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
    UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);

    //Image tag
    TagBuilder imageTag = new TagBuilder("img");
    imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
    imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);

    //Anchor tag
    TagBuilder anchorTag = new TagBuilder("a");
    anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
    anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
    anchorTag.MergeAttributes(new RouteValueDictionary(ajaxOptions), false);
    anchorTag.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes), false);

    return MvcHtmlString.Create(anchorTag.ToString());
}
我已尝试调试该问题,因此比较了从普通方法和扩展方法生成的Html:

正常(工作):


分机:

<a allowcache="False" confirm="" httpmethod="" insertionmode="Replace" loadingelementduration="0" loadingelementid="" onbegin="" oncomplete="" onfailure="" onsuccess="" updatetargetid="Edit" url="" href="/Admin/Event/Edit/1"><img src="/Content/Images/edit-icon.png"></a>


呈现的Html是不同的,因为普通方法使用Html
data-*
属性,而另一种方法不使用。我不知道如何解决这个问题。

您需要使用
AjaxOptions
.ToUnobtrusiveHtmlAttributes()
方法来生成正确的
数据ajax-*
属性。你的方法应该是

public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
    object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
    UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);

    //Image tag
    TagBuilder imageTag = new TagBuilder("img");
    imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
    imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);

    //Anchor tag
    TagBuilder anchorTag = new TagBuilder("a");
    anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
    anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));

    // change the following line
    anchorTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
    // recommend the following change
    anchorTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(linkHtmlAttributes)));

    return MvcHtmlString.Create(anchorTag.ToString());
}
旁注:请参阅
.ToUnobtrusiveHtmlAttributes()
方法

public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
    object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
    UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);

    //Image tag
    TagBuilder imageTag = new TagBuilder("img");
    imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
    imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);

    //Anchor tag
    TagBuilder anchorTag = new TagBuilder("a");
    anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
    anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));

    // change the following line
    anchorTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
    // recommend the following change
    anchorTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(linkHtmlAttributes)));

    return MvcHtmlString.Create(anchorTag.ToString());
}