view raw
Ty53ler Ty53ler - 9 months ago 91
ASP.NET (C#) Question

MVCReportViewer Help - Cannot Pass Parameters To Report - ViewBag Issue

Not sure if this is the place to ask or if anyone has experience working with MVCReportViewer ( but I am struggling with getting it to work with my application. I simply want to pass some parameters to the report and the report URL to the viewer but it is not working. If I type the expected URL and parameters it works but anything dynamic is not working...

Here is how I am calling the reportviewer:

@Html.MvcReportViewerFluent(pReportURL).ReportParameters(new { StartingItem = pStartingItem, EndingItem = pEndingItem }).Attributes(new { Height = 500, Width = 1200, style = "border:none; background-color:white;" }).ControlSettings(new ControlSettings { ShowParameterPrompts = false })

While I am getting the values for the variables with ViewBags.

var pReportURL = (string)ViewBag.reportURL;
var pStartingItem = (string)ViewBag.StartingItem;
var pEndingItem = (string)ViewBag.EndingItem;

I set up some breakpoints and the variables are getting assigned the values but it seems they are never getting passed into the reportviewer. I just get an error that says the "Report is not specified" even though I can see the value of the URL with my breakpoints. Am I declaring these correctly? I am new to MVC and this seemed like the easiest way to get this to work but I am now struggling.

EDIT1: So after messing around some more the issue is not with the reportviewer but with how I am using my variables. If I declare my "var pReportURL = 'Report/ThisReport'" it will work. So even though in my breakpoints my ViewBags are the correct value I must be using them incorrectly... What do I need to do to use ViewBags values as variables in my view or something similar.


Following up this question for anyone who uses MvcReportViewer and is struggling with a similar issue... Not sure if this is the best way to get this to work but it worked for me so I thought I would share.

From reading the docs I was able to find an example of someone who was doing something similar with passing values from their controller to the view and then using those values as parameters for the report.

In my controller I passed some values to the view as TempData using the following syntax

public ActionResult CurrentBomReport(CurrentBomViewModel model)

//do some logic first...
//declare parameters as tempdata.
TempData["reporturlparam"] = model.reporturlparam;
TempData["reportparam1"] = model.reportparam1;
TempData["reportparam2"] = model.reportparam2;

return View(model);

Then in my view...

string reporturl = (string)TempData["reporturlparam"];

    var parameters = new List<KeyValuePair<string, object>>
    new KeyValuePair<string, object>("NameOfReportParam1", (string)TempData["reportparam1"]),
    new KeyValuePair<string, object>("NameOfReportParam2", (string)TempData["reportparam2"])

@Html.MvcReportViewerFluent(reporturl).ReportParameters(parameters).Attributes(new { Height = 500, Width = 500, style = "border:none; background-color:white;" }).ControlSettings(new ControlSettings { ShowParameterPrompts = false })

This seemed to be the most straight forward method to get this to work.