简体   繁体   English

在MVC4视图中显示图像

[英]Display Image in MVC4 View

Refering this link, I created an QR code image using MessagingToolkit.QRCode.dll. 引用此链接,我使用MessagingToolkit.QRCode.dll创建了QR码图像。 How to display the saved image in the same view with the myLayout. 如何在与myLayout相同的视图中显示保存的图像。
Controller Code 控制器代码

  [HttpPost]
    public ActionResult GenerateExcelQR(string Command, FormCollection collection)
    {
    if (Command == "Excel")
    {
    //  logic to generate Excel
    }
    else if(Command =="QRCode")
     // qr code logic //
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap img = encoder.Encode(qrcode);
        string path = Server.MapPath("/Images/QRCode.jpg"); 
        img.Save(path, ImageFormat.Jpeg);
        return base.File(path,"image/jpeg"); // Displays the QR image without my layout.
     // return View(path,"image/jpeg");      // Returns an error specifying "Images\QRCode.jpg' or its master was not found or no view engine supports the searched locations."
    }

How to display the QRCode image in the Same View with the layout. 如何在与布局相同的视图中显示QRCode图像。
Any suggestions. 有什么建议么。
EDIT ImageModel.cs 编辑 ImageModel.cs

public static class ImageModel
    {
        public static string Image(this HtmlHelper htmlHelper, string path, string alt)
        {
            var img = new TagBuilder("img");
            img.MergeAttribute("src", path);
            img.MergeAttribute("alt", alt);
            return img.ToString(TagRenderMode.SelfClosing);
        }
    }

In view 视野中

@model IEnumerable<SampleECommerce.Models.CheckoutModel> // reference to 1st model to return values
@using SampleECommerce.Models; // for Imagemodel

You should return not a file, but a view, and in view render your image. 您不应返回文件,而应返回视图,并在视图中渲染图像。 Here Image - is a html helper: 此处的图片-是html帮助器:

public static HtmlString Image(this HtmlHelper helper, string path, string alt)
{
    var img = new TagBuilder("img");
    img.MergeAttribute("src", path);
    img.MergeAttribute("alt", alt);
    return new HtmlString(img.ToString(TagRenderMode.SelfClosing));
}

And then just use it in a view: 然后在视图中使用它:

@Html.Image(path_to_your_image, "Description")

And in your controller use just return View(); 并在您的控制器中使用,只需return View();

To use this helper, you should create a folder in your project, for example 'Helpers'. 要使用此帮助程序,应在项目中创建一个文件夹,例如“ Helpers”。 Then, create file in this folder, for example 'HtmlImageHelper.cs', then put the content of helper in this file. 然后,在此文件夹中创建文件,例如“ HtmlImageHelper.cs”,然后将帮助程序的内容放入此文件中。 After this, in your view at the top write: using YourProjectName.Helpers . 之后,在顶部的视图中输入: using YourProjectName.Helpers After this you are able to use this html helper to render images 在此之后,您可以使用此html助手来渲染图像

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

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