-1

我对一件事感到好奇。假设我将图像保存在 SQL 数据库中(我知道不推荐这样做;最好的方法是只保存对保存在其他地方的图像的引用,但我想问你一些关于这个特定情况的问题)。

我正在提供这样的文件:

 public ActionResult Serve(int id)
    {
        ......
        return File(img.Content, img.ContentType);
    }

我还做了一个Html helper

   public static HtmlString ServeImage(this HtmlHelper html, int id)
    {
        var urlHelper= new UrlHelper(html.ViewContext.RequestContext);

        var tag= "<img  src='{0}' width='200' height='200' />";

        return new HtmlString(string.Format(imageTag, urlHelper.Action("Serve", "Image", new { id = id })));
    }

所以,当我想展示一张图片时,我正在写一个类似这样的视图:@Html.ServeImage(imageId)

我的问题是:*有什么方法可以urlHelper.Action("Serve", "Image", new { id = id })))通过 ajax 调用并且仍然使用我的助手?*

我已经阅读了有关 Ajax Helpers 的信息,但我认为这对我没有帮助,我只剩下一个选择。我需要放弃我的助手并像往常一样使用 ajax 调用我的操作。这是正确的吗?

我是说:

$.ajax(function() { ..... });

4

1 回答 1

0

您可以只使用普通的 img 标签并将 src 设置为 @urlHelper.Action("Serve", "Image", new { id = id })))

例子:

<img src='@urlHelper.Action("Serve", "Image", new { id = id })' width='200' height='200' />
于 2013-07-08T12:45:38.087 回答