Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:24:45+00:00 2026-05-26T15:24:45+00:00

A Model contains, amongst other properties, a method which returns an MSChart as a

  • 0

A Model contains, amongst other properties, a method which returns an MSChart as a MemoryStream.

In my View I copy the MemoryStream to TempData[“Chart”] and then use URL.Action() to call a controllers action to return a FileContentResult using the MemoryStream from TempData.

In the Model

public MemoryStream ViewerChart()
{
    Chart chart = new Chart();
    :
    :
    using (MemoryStream memStream = new MemoryStream())
    {
        chart.SaveImage(memStream, ChartImageFormat.Jpeg);
        return memStream;
    }
}

In the View

@{
    TempData["Chart"]= Model.ViewerChart();
 }
<img alt="Chart" src="@Url.Action("RenderChart")" />

In the Controller

 public ActionResult RenderChart()
 {

   MemoryStream ms = TempData["Chart"] as MemoryStream;
   return File(ms.ToArray(), "image/jpeg");
 }

Despite working OK, this all seems a bit nasty to me, particularly the use of TempData

Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:24:46+00:00Added an answer on May 26, 2026 at 3:24 pm

    The acedemic way would be to retrieve the model object again in the RenderChart action and return the chart image to the view.

    But i think its a valid approach to store the image in TempData for reducing database roundtrips and boosting performance.

    Maybe its even unnecessary to improve the performance. The database will have the data available in its cache when reading it the second time and the additional 10ms needed to fetch the data again might not be noticeable by the user.

    At least TempData seems to be the right store because the value is deleted from the collection when its read by the RenderChart action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The ThingController creates a model which contains (amongst other properties) a collection of Things.
I have a model that contains a foreign key value, then in the form
I have a model that contains one-to-one fields to other models. I over rid
I have a jQuery Model windows which contains a form. On creating of the
My model contains an enum, which are mapped by JPA to an int column
My Core Data model contains an entity, Shape, that has two self-referential relationships, which
I am constructing a page using Editor Templates and composition. My view model contains
My model contains a class Section which has an ordered list of Statics that
My model contains a string field called longdescription which gets the value of the
I have a view model for a view which displays multiple forms on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.