2

My way to ASP.NET MVC was not across ASP.NET Web Forms, so it's hard for me to understand how better to pass value from ASP.NET MVC controller to ASP.NET webforms script which is inside MVC View.

For example, controller action:

 public ViewResult Print(string id)
    {
       ...
       string myvar = "realvalue"; 
       return View();
    }   

My view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
...
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script runat="server">
      ...  
     string par1 = "stubvalue";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        ReportViewer1.Report = new MyProj.Web.Reports.Rep1(par1);
    }
    </script>
<telerik:ReportViewer runat="server" ID="ReportViewer1" />
</asp:Content>

How to assign a value of myvar from a controller action to par1 variable in the View instead "stubvalue"?

rem
  • 16,745
  • 37
  • 112
  • 180

3 Answers3

1

Have you tried passing it through ViewData, like so?

in the action: ViewData["myvar"] = "realvalue";

in the view: string parl = ViewData["myvar"];

Tahbaza
  • 9,486
  • 2
  • 26
  • 39
  • This way I'm getting a compilation error: Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.Mvc.ViewPage.ViewData.get' – rem Dec 24 '10 at 16:47
  • Your approach works with a correction @user6130 suggested. Thanks, +1 – rem Dec 24 '10 at 19:57
1

I would iframe in the webform bit rather than include it directly -- there are lots of ways it could break depending on the component. Then you could pass the value over the query string.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
1

Move the setter call to onload event

protected override void OnLoad(EventArgs e) { base.OnLoad(e); string par1 = (string) ViewData["myvar"]; ReportViewer1.Report = new MyProj.Web.Reports.Rep1(par1); }

chandmk
  • 3,496
  • 3
  • 21
  • 26