weixin_33682790 2016-07-06 22:04 采纳率: 0%
浏览 93

图像按钮Ajax.ActionLink

I am trying to create an extension of the Ajax.ActionLink. I have done the same to Html.ActionLink and its working but when i tried to make an extension of the Ajax method, its not working. What happens is that when i click on the link, it doesn't render my partial view (replace) but instead it redirect me to the view. I want to note that stranded Ajax.ActionLink method is working fine

Here is my code:

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());
}

I have tried to debug the issue, so i have compared both the generated Html from both the normal and the extension method:

Normal (Working):

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#Edit" href="/Admin/Event/Edit/1">Edit</a>

Extension:

<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>

The rendered Html is different as the normal method uses the Html data-* attributes while the other not. I am not sure how to solve this issue.

  • 写回答

1条回答 默认 最新

  • weixin_33720956 2016-07-07 01:57
    关注

    You need to use the .ToUnobtrusiveHtmlAttributes() method of AjaxOptions to generate the correct data-ajax-* attributes. Your method should be

    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());
    }
    

    Side note: Refer source code for the .ToUnobtrusiveHtmlAttributes() method

    评论

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,