NS.X.
NS.X.

Reputation: 2142

Charting without using controller action in ASP.NET MVC

I know how to chart by using a Controller action that returns FileResult.

My question is, is it possible to move the action into the view (cshtml) file as a helper? This way the view logic is entirely contained in the view.

The problem is, without a Controller action, what URL can I fill in the img src attribute? Or is there a different way, instead of img tag, to display the image stream?

Upvotes: 0

Views: 327

Answers (1)

haim770
haim770

Reputation: 49095

If the core problem is that you want to load image data without a url (whether that url points to a Controller, or some static resource), you can dump the image data directly to the html using base64 embedding as follows:

public static MvcHtmlString EmbedImageWithBase64(this HtmlHelper helper, byte[] imageBytes)
        {
        var base64Data = Convert.ToBase64String(imageBytes);
        var imageSrcData = "data:image/png;base64," + base64Data;
        return new MvcHtmlString(string.Format("<img alt="Chart Data" src=\"{1}\" />", imageSrcData));
        }

Upvotes: 3

Related Questions