title: Finding, loading and adding partial views dynamically
body: <p>I've been tasked with converting an existing webforms application to mvc 3 razor.</p>
  
  <p>The application currently has an aspx page which has a static header user control and "n" amount of other user controls which are dynamically created. In the code behind for the file, it is executing the below code in various specific sections to dynamically process user controls with information provided from the database.</p>
  
  <p>I know how to statically create partial views, but being somewhat new to MVC, how would I go about defining this new "aspx" page and also to dynamically find, load and add the partial views (each equivalent to the below webforms code)?</p>
  
  <p>btw, the code will be in C# as well.</p>
  
  <pre><code>Dim parent As Control = Page.FindControl(_moduleSettings.PaneName)
  
   Dim portalModule As PortalModuleControl = CType(Page.LoadControl(_moduleSettings.DesktopSrc), PortalModuleControl)
  
     parent.Controls.Add(portalModule)
  </code></pre>
  
  <p>I think I can do something like this when the page is rendering. I want to make it as simple as possible. </p>
  
  <p>The "PaneName" will be set in the parent variable which determines where in the page it will be shown (Left, Right, or Main)</p>
  
  <p>The "DeskTopSrc" is the name of the partial view to display.</p>
  
  <p>So, take the code out of the code behind and place it in the main View. Perform the above processing logic in the View (boy, switching from aspx code behind to a View throws me a loop. I gotta get use to doing the processing in the View. Reminds me of Classic ASP, but the Razor syntax will help).</p>
  
  <p>Display the partial view via the @Html.PartialView('partial view name'). This view might have a grid in it associated with a specific model.</p>
  
  <p>Below is the part I am unsure about. </p>
  
  <p>I've done database processing for a main View associated with a Controller, but not with a partial view that needs to do some database processing.</p>
  
  <p>Perform any database processing logic (if any) for this partial view in the Controller associated with the main View (which contains this partial View). </p>
  
  <p>In the Action Index method while looping over these "partial views", I can get the data and display the views....</p>
  
  <p>Ahhh, I think I got it.....</p>
  
  <p>After carefully thinking it through, if someone could help me out with the last statement here, I would greatly appreciate it.</p>
  
  <p>1.Have partial views already statically created with the specific HTML markup that I need in the Views/Shared folder.</p>
  
  <p>2.In the main View, I will already have </p>
  
  <pre><code>@Html.Partial(ViewData["partial_view_left"]) 
  @Html.Partial(ViewData["partial_view_right"])
  @Html.Partial(ViewData ["partial_view_main"]) 
  </code></pre>
  
  <p>statements in specific locations of the HTML which will render the partial views as I retrieve their names from the database.</p>
  
  <p>3.In the Controller's Index method, I need to do the following:</p>
  
  <p>a) Loop through the converted logic (from the CodeBehind of the existing WebForms page in the PageLoad event) in the Index action method of the new Controller which will load the partial views dynamically.</p>
  
  <pre><code>1) Find out where the partial view will be displayed (left, right, main) from the database via the "parent" variable.
  
  2) Find out the name of the partial view that will be displayed from the database via the “DesktopSrc” variable.
  </code></pre>
  
  <p>eg: ViewData["partial_view_left"] = "left_view";    OR</p>
  
  <pre><code>    ViewData["partial_view_right"] = "right_view"; OR
  
      ViewData["partial_view_main"] = "main_view";
  </code></pre>
  
  <p>3) Right here is where I am unsure of how to properly display the partial view.</p>
  
  <p>I need to have the equivalent of a webforms "Controls.Add" method to render each partial view from the Controller that I retrieve from
  the database from step 3.a.2</p>
  
  <pre><code>   What statement can I use in this Index method of the Controller that will accomplish this?
  </code></pre>
  
  <p>In other words, if I dynamically need to display several partial views inside of a parent view, how is this accomplished in MVC? </p>
  
  <p>I know for each partial view, I can send over the model associated with it, but I just don't know how I can place several partial views inside the main view page at run time from one Action method.</p>