How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?

浪尽此生 提交于 2019-12-11 02:52:19

问题


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"?


回答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); }




回答2:


Have you tried passing it through ViewData, like so?

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

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




回答3:


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.



来源:https://stackoverflow.com/questions/4527123/how-to-pass-a-value-from-asp-net-mvc-controller-to-asp-net-webforms-control-insi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!