如何将SQL结果存储在ASP.NET MVC中

时间:2015-07-20 12:32:00

标签: asp.net asp.net-mvc

..这样我就可以在_Layout文件中显示queryresult的特定行。 结果是随机排序的每页只需要一个Databaserequest请求

在_Layout.cshtml中,(我希望这两种方法显示相同的内容):

 @{ Html.RenderAction("RegularAdV2", "AdV2", new { type = "panoramaxl", skip = 0 });}

@{ Html.RenderAction("RegularAdV2", "AdV2", new { type = "panoramaxl", skip = 0 });}

Childaction:

[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 => 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 => new AdViewModel { Script = x.AdExtra, ClickUrl = x.AdClickUrl, Place = x.PlacementName, Src = x.AdPath, Title = x.AdTitle }).Skip(skip).FirstOrDefault();

                 }
             }

                 return PartialView(ad);


         }

部分视图:

@model Jaff.WebUI.Models.AdViewModel

@if (Model != null)
{
    if (string.IsNullOrEmpty(Model.Script))
    {
        <p class="ad-presentation">Annons</p>
        <a href="@Model.ClickUrl" target="_blank"><img alt="annons" class="ad-image" src="//ads.-------.se/AdFiles/img/@Model.Src"></a>
    }
    else
    {
        <p class="ad-presentation">Annons</p>
        <div class="script-box">@Html.Raw(Model.Script)</div>
    }
}

视图模型:

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; }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议你可以使用Session变量来存储你的sql结果,然后再重用它。 例如:Session [“SQLQueryData”] = result