Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# 在html助手中通过ajax提供图像_C#_Asp.net Mvc_Asp.net Mvc 4_Html Helper - Fatal编程技术网

C# 在html助手中通过ajax提供图像

C# 在html助手中通过ajax提供图像,c#,asp.net-mvc,asp.net-mvc-4,html-helper,C#,Asp.net Mvc,Asp.net Mvc 4,Html Helper,我对一件事很好奇。假设我正在将图像保存到SQL数据库中(我知道不建议这样做;最好的方法是只保存对保存在其他地方的图像的引用,但我想问您一些有关此特定情况的问题) 我提供的文件如下: public ActionResult Serve(int id) { ...... return File(img.Content, img.ContentType); } 我还制作了一个Html助手: public static HtmlString Se

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

我提供的文件如下:

 public ActionResult Serve(int id)
    {
        ......
        return File(img.Content, img.ContentType);
    }
我还制作了一个
Html助手

   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 })));
    }
publicstatichtmlstringserveImage(此htmlhelp-html,int-id)
{
var urlHelper=新的urlHelper(html.ViewContext.RequestContext);
var tag=“”;
返回新的HtmlString(string.Format(imageTag,urlHelper.Action(“服务”,“图像”,new{id=id}));
}
因此,当我想显示一张图片时,我正在这样的视图中编写:
@Html.ServeImage(imageId)

我的问题是:*有没有办法通过ajax调用
urlHelper.Action(“服务”,“图像”,新的{id=id}))
而仍然使用我的助手*

我读过关于Ajax助手的文章,但我认为这对我没有帮助,我只有一个选择。我需要放弃我的助手,像往常一样用ajax调用我的操作。是这样吗

我的意思是:

$.ajax(函数(){
.....

});

您只需使用一个普通的img标记并将src设置为@urlHelper.Action(“服务”,“图像”,新的{id=id})))

例如:

<img src='@urlHelper.Action("Serve", "Image", new { id = id })' width='200' height='200' />


为什么需要AJAX调用?你到底想达到什么目的?您可以对控制器操作进行AJAX调用,该操作返回一个局部视图,其中包含对自定义帮助程序
@Html.ServeImage(imageId)
的调用。如果有一种方法可以使用AJAX调用Html帮助程序,我会很好奇。只是为了学习新东西。无论如何,你说的有道理:)。我也可以。不,没有办法用AJAX调用HTML助手。这没有道理。AJAX用于调用服务器端端点(在ASP.NET MVC中称为控制器操作)。这与我的问题有什么关系?