2011-12-15 56 views
0

快速但愚蠢的问题。这种级联有点儿。如果我在aspx页面上创建一个变量,它是否可用于我的aspx部分呈现的ascx。另外,aspx页面如何处理这些变量?我知道一个事实,那些变量比说TempData或ViewData变量的持续时间要长得多。下面是我想知道的一个小例子MVC上的变量生命周期页面

<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" })%> 

注意我已经创建的变量。他们会持续多久?我现在可以在下面的ascx页面中使用这些变量吗?

+1

这是今天我见过的最糟糕的代码。你应该把所有的逻辑移动到你的控制器或者视图模型中(取决于逻辑的作用) – jgauffin 2011-12-15 15:17:07

回答

1

这些变量只能在aspx页面的范围内使用。通过RenderPartialRenderAction呈现的任何页面都需要定义要使用的自己的一组变量。如果您需要在aspx页面和ascx页面之间共享数据,那么您需要将模型或ViewData转换为RenderPartial视图并以这种方式访问​​数据。

0

您也可以通过模型的局部视图<%:部分(“PartialViewName”,模型)%>