0

I have to make a statistical project in C # MVC which contains several steps requiring different renderings for the user (browser, PDF document, Excel, ...) For browser part, I currently have a complex class containing a collection that serves as model for a view that displays either a table, a Chart or both.

I use @url.action to get an image of the chart I have defined a RouteValueDictionary to pass the object in url.action arguments

src="@Url.Action("SampleChart", "Statistique", Model.RouteValues ))"

In the controller: boolean and string are filled but not the dictionary(resultatsStatistique). Do you have any idea to resolve my problem ?

Below is a simplified version of the Model

    public class StatisticDTO
{
    public virtual string Stat_Title { get; set; }
    public bool DisplayChart { get; set; }
    public bool DisplayTable { get; set; }
    public Dictionary<string, RowStat> resultatsStatistique;
    public RouteValueDictionary RouteValues
    {
        get
        {
            var rvd = new RouteValueDictionary();
            rvd["DisplayChart"] = DisplayChart;
            rvd["DisplayTable"] = DisplayTable;
            rvd["resultatsStatistique"] = resultatsStatistique;
            return rvd;
        }
    }
}
public class RowStat
{
    public virtual string prest_libe { get; set; }
    public virtual decimal prest_effec { get; set; }
    public virtual int prest_order { get; set; }
    public virtual string prest_sex { get; set; }
} 

Below is a simplified version of my view

    @model StatisticDTO

<div>
    @if (Model.DisplayChart )
    {
        <div>
             <img  src="@Url.Action("SampleChart", "Statistique", Model.RouteValues ))" />
         </div>
   }


    @if (Model.DisplayTable)
    {
        @Html.Partial("_Tableau", Model)
    }

</div>

Below is a simplified version of the controller

public ActionResult SampleChart(StatisticDTO modelIN)
               {
(.....)
MemoryStream ms = new MemoryStream();
            chart.SaveImage(ms, ChartImageFormat.Png);
            return File(ms.ToArray(), "image/png");
}
Uliat
  • 31
  • 6
  • It would be helpful to see what is rendered to the browser as the url for the image. – MikeS Feb 03 '17 at 14:52
  • where is the Get of the view in the controller ? – Ahmed Feb 03 '17 at 14:53
  • cant use `@Url.Action("SampleChart", "Statistique", new { modelIN = Model })`? – JamieD77 Feb 03 '17 at 15:08
  • no modelIN is Null – Uliat Feb 03 '17 at 15:11
  • You cannot. The `Url.Action()` method (and all methods that build a `RouteValueDictionary`) use the `.ToString()` of the property name to generate the query string. It does not use recursion on complex properties. –  Feb 03 '17 at 21:54
  • You do not pass a complex model to a GET method (unless it contains only a few simple properties). You pass the ID of the object, and then get that object again in the GET method based on that ID –  Feb 03 '17 at 21:57

1 Answers1

0

Okay. Thanks for your comments. May be there is no simple solution. So I looked for another solution to get my image source. It's off topic here, but for those who are interested, the solution is not to do a round trip between 2 actions. You have to generate a file in the first action and pass it to the model as a byte array

Like this in the first action

ms = new MemoryStream();
Chart.SaveImage(ms, ChartImageFormat.Png);
modelOUT.ByteArray = ms.ToArray();

and this in view

@{
    var base64 = Convert.ToBase64String(Model.ByteArray);
    var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}

<img src="@imgSrc" />

MVC How to display a byte array image from model

Community
  • 1
  • 1
Uliat
  • 31
  • 6