ajax.actionlink helper问题,返回字符串

时间:2011-03-02 10:11:21

标签: asp.net-mvc asp.net-mvc-3 razor

我正在使用带Razor的MVC3。 我有以下帮助:

public static class ImageActionLinkHelper
    {
        public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, object routeValues, AjaxOptions ajaxOptions)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("alt", "");
            var link = helper.ActionLink(builder.ToString(TagRenderMode.SelfClosing), actionName, routeValues, ajaxOptions);
            return link.ToHtmlString();
        }
    }

在我看来,我有:

@Ajax.ImageActionLink("../../Content/Images/button_add.png", "JobTasksNew", "TrackMyJob",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "tmjDynamic" }))

这就是我在呈现页面时得到的结果

   <a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#tmjDynamic" href="/TrackMyJob/JobTasksNew?Length=10"><img alt="" src="../../Content/Images/button_add.png"></img></a>

Microsoft有 ajax.actionlink.Replace 的示例,但我没有此方法。 你能帮我搞清楚正确的html字符串吗?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

请试试这个:

public static class ImageActionLinkHelper {
        public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, object routeValues, AjaxOptions ajaxOptions) {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("alt", "");
            var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions);
            var html = link.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
            return new MvcHtmlString(html);
        }
    }