title: How do I store SQL result in ASP.NET MVC
body: <p>..so that I can show specific lines of queryresult in _Layout file. <strong>The result is ordered randomly</strong>. <em>Want only one Databaserequest per pagerequest</em>. </p>
  
  <p>In _Layout.cshtml, (I want these two methods to show the same):</p>
  
  <pre><code> @{ Html.RenderAction("RegularAdV2", "AdV2", new { type = "panoramaxl", skip = 0 });}
  
  @{ Html.RenderAction("RegularAdV2", "AdV2", new { type = "panoramaxl", skip = 0 });}
  </code></pre>
  
  <p>Childaction:</p>
  
  <pre><code>[ChildActionOnly]
           public PartialViewResult RegularAdV2(string type,int skip)
           {
  
  
               AdViewModel ad = null;
               if ("mobile".Equals(type))
               {
                   if (UserAgentHelper.CheckUserAgent((string)Request.UserAgent, Request.Browser.IsMobileDevice))
                   {
                       ad = repository.MyAds(type, 3, true).Select(x =&gt; new AdViewModel { Script = x.AdExtra, ClickUrl = x.AdClickUrl, Place = x.PlacementName, Src = x.AdPath, Title = x.AdTitle }).Skip(skip).FirstOrDefault();
  
                   }
               }
               else
               {
                   if (!UserAgentHelper.CheckUserAgent((string)Request.UserAgent, Request.Browser.IsMobileDevice))
                   {
                      ad = repository.MyAds(type, 3, false).Select(x =&gt; new AdViewModel { Script = x.AdExtra, ClickUrl = x.AdClickUrl, Place = x.PlacementName, Src = x.AdPath, Title = x.AdTitle }).Skip(skip).FirstOrDefault();
  
                   }
               }
  
                   return PartialView(ad);
  
  
           }
  </code></pre>
  
  <p>The partial view:</p>
  
  <pre><code>@model Jaff.WebUI.Models.AdViewModel
  
  @if (Model != null)
  {
      if (string.IsNullOrEmpty(Model.Script))
      {
          &lt;p class="ad-presentation"&gt;Annons&lt;/p&gt;
          &lt;a href="@Model.ClickUrl" target="_blank"&gt;&lt;img alt="annons" class="ad-image" src="//ads.-------.se/AdFiles/img/@Model.Src"&gt;&lt;/a&gt;
      }
      else
      {
          &lt;p class="ad-presentation"&gt;Annons&lt;/p&gt;
          &lt;div class="script-box"&gt;@Html.Raw(Model.Script)&lt;/div&gt;
      }
  }
  </code></pre>
  
  <p>viewmodel:</p>
  
  <pre><code>namespace Jaff.WebUI.Models
  {
      public class AdViewModel
      {
          public string Src { get; set; }
          public string Place { get; set; }
          public string Title { get; set; }
          public string ClickUrl { get; set; }
          public string Script { get; set; }
      }
  }
  </code></pre>