Sebastian
Sebastian

Reputation: 4811

Return image content from controller to view MVC3

I want a controller to serve a chart control and use that in View directly . What i did is in Razor i used

<p>
    <img src="/Exam/ChartReport" />
</p>

and in Controller i created an action method like

public ActionResult ChartReport()
{
    var data = new System.Collections.ArrayList
               {
                   new { X = DateTime.Now.AddMonths(-2), Y = 200 },
                   new { X = DateTime.Now.AddMonths(-1), Y = 300 },
                   new { X = DateTime.Now, Y = 500 }
               };
    new System.Web.Helpers.Chart(400, 200, System.Web.Helpers.ChartTheme.Blue)
        .AddTitle("Price enquiries")
        .DataBindTable(data, "X")
        .Write("png");
    return null;
}

Now what i want is to pass the model as it is from View to the Controller action as parameter , so i can create chart without making any Db queries . How can i do this . My View is created based on model

@model MyApp.ViewModels.SampleModel
<p>
   <img src="/Exam/ChartReport" />
</p>

I tried to do

<p>
    < img src="/Exam/ChartReport/@Model" />
</p> 

and in controller

public ActionResult ChartReport(SampleModel model) //But model is null
{
}

Upvotes: 0

Views: 1042

Answers (1)

Jatin patil
Jatin patil

Reputation: 4288

You can pass parameters to your controller action as follows:

<img ... src="/Exam/ChartReport?parameter1=value1&parameter2=value2" />

And your action should be as follows:

public class ExamController : Controller
{
    public ActionResult ChartReport(string parameter1, string parameter2)
    {
         ...
         // Return the contents of the Stream to the client
         return File(imgStream, "image/png");
    }
}

Upvotes: 1

Related Questions