Search code examples
asp.net-mvcvariableslifecycle

Variable Life Cycle on MVC page


Quick but dumb question. That kind of cascades a little bit. If I create a variable on an aspx page, will it be available to an ascx that my aspx partially renders. Also, How does the aspx page handle those variables? I know for a fact, those variables last a lot longer than say a TempData or ViewData variable. Here is a small example of what I am wondering about...

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h3><% Response.Write(((ViewData["MESSAGE"] == null) ? "Edit Encounter Information" : ViewData["MESSAGE"] ));            
       int encounter = int.Parse((string)TempData["ENCOUNTID"]);
       int PopPatId = int.Parse((string)TempData["POPPATID"]);
       int PatID = int.Parse((string)TempData["PATID"]);
       int population = (int)TempData["POPULATIONID"];
       string templates1 = (string)TempData["TEMPLATES"];
       string MRN = ((TempData["MRN"] == null) ? "Add Patient":(string)TempData["MRN"]);
       ViewData["TEMPLATES"] = templates1;         
    %>        
    </h3>   
    <div id="countdownDiv" style="display:none">
    <p><font color="#990000"><b>Sorry, This Data has expired. Please Refresh The page.</b></font></p>    
    <%
        Html.BeginForm("EditTemplate", "PatientACO", new { Template = PatID, popID = population, PopulationPatID = PopPatId, Enc = encounter });
        TempData["TEMPLATES"] = templates1;
        %><input type="submit" value="Refresh" id="test" /><%
        Html.EndForm();%>     
</div>   
<% Html.RenderPartial("_EditTemplate"); %>
<%: Html.ActionLink("Back", "TemplateInfo", new { PopulationID = population, ActiveAll = "1" })%>

Note I have created variables. How long will they last? can I use those variables now in the ascx page that I call below?


Solution

  • Those variables can only be used within the scope of the aspx page. Any page rendered via RenderPartial or RenderAction will need to define their own set of variables to use. If you need to share data among the aspx page and the ascx page then you need to pass a model or ViewData to your RenderPartial view and access the data that way.