简体   繁体   English

Asp.net Mvc2 Ajax ImageActionLink帮助器

[英]Asp.net Mvc2 Ajax ImageActionLink Helper

I'm fairly new to Mvc and ran into a problem try to use an Image as an Ajax Action link. 我是Mvc的新手,尝试使用Image作为Ajax Action链接时遇到问题。 I found a helper that I believe was posted by Stephen Walther... 我找到了我认为是Stephen Walther发布的助手。

using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace Helpers
{
    public static class ImageActionLinkHelper
    {

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

    }
}

However the "Replace" method does not exist in the version I'm using. 但是,“ Replace”方法在我使用的版本中不存在。 (maybe an Mvc2 issue since I believe his contact manager app was an MVC1 app??) A quick search through the docs and I found the "Replace" method as obsolete and it was suggesed to use "MvcHtmlString.Create()" I tried a number of combinations in this helper using that method and I could not get the link to render with an image. (也许是Mvc2问题,因为我认为他的联系人管理器应用程序是MVC1应用程序?使用该方法的此帮助器中的许多组合,我无法获得使用图像进行渲染的链接。 I would either get a link pointing to the correct action/controller and the img tag as plain text OR vice versa a correctly rendered img with the link as plain text. 我要么得到一个指向正确动作/控制器的链接,然后将img标签标记为纯文本,反之亦然,将链接正确显示为img的文本标记为纯文本。

Just as a sidenote on almost all the combos I tried, I was returning a type MvcHtmlString instead of the standard string listed in this particular helper. 正如我尝试过的几乎所有组合的注解一样,我返回的是MvcHtmlString类型,而不是此特定帮助程序中列出的标准字符串。

This is probably from when MvcHtmlString didn't exist yet. 这可能是从MvcHtmlString不存在的时候MvcHtmlString You should be able to make this work with Replace() method tho, just need to get the actual string it was supposed to work with in the first place: 您应该能够使用Replace()方法来完成这项工作,只需要首先获取应该使用的实际字符串即可:

var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToString();
return link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM