C# 在MVC视图中将列表声明为静态
我正在按照我的要求加载更多数据功能。我第一次显示了12条记录,对我来说效果很好,但每当我试图显示其余数据时,较早的数据就会丢失,当前的数据就会显示出来 为此,我使用了一个来自代码隐藏的列表,但我的问题是我在视图上声明了该列表,该列表每次都被初始化,然后每次在数据绑定时变为空,这就是早期数据丢失的原因 为此,我不能使用AJAX 这是我的看法C# 在MVC视图中将列表声明为静态,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我正在按照我的要求加载更多数据功能。我第一次显示了12条记录,对我来说效果很好,但每当我试图显示其余数据时,较早的数据就会丢失,当前的数据就会显示出来 为此,我使用了一个来自代码隐藏的列表,但我的问题是我在视图上声明了该列表,该列表每次都被初始化,然后每次在数据绑定时变为空,这就是早期数据丢失的原因 为此,我不能使用AJAX 这是我的看法 @model List<ChangiRecommends.BAL.ViewModel.Product.ProductViewModel> @usi
@model List<ChangiRecommends.BAL.ViewModel.Product.ProductViewModel>
@using ChangiRecommends.BAL.ViewModel.Product;
@using ChangiRecommends.Utilities;
@{
if(ViewBag.productCount > 12)
{
}
List<ProductViewModel> productList = new List<ProductViewModel>();
if(productList.Count == 0)
{
productList = Model;
}
else
{
productList.AddRange(Model);
}
ViewBag.Title = "destination";
Layout = "~/Views/Shared/_Layout.cshtml";
var key = ViewBag.Key;
var searchingKey = ViewBag.searchingkey;
var skip = ViewBag.skip;
}
<div class="destination_pics">
<div class="row">
@if (productList.Count>0)
{
int count = 0;
foreach (var item in productList)
{
if (count == 0)
{
<div class="col-sm-8">
<div class="highlights">
<a href="@Url.Action("tourdetail", "customer",new {key = QueryStringManager.Encrypt(item.ID)})">
<div class="highlithsTxt"> <p><strong>$@item.OnlinePrice</strong></p> <p>@item.ProductDisplayName</p> </div>
<img class="img-fluid" src="@item.ProductImage">
</a>
</div>
</div>
}
else if (count > 0 && count <= 2)
{
if (count == 1)
{
@:<div class="col-sm-4">
}
<div class="highlights">
<a href="@Url.Action("tourdetail", "customer",new {key = QueryStringManager.Encrypt(item.ID)})">
<div class="highlithsTxt"> <p><strong>$@item.OnlinePrice</strong></p> <p>@item.ProductDisplayName</p> </div>
<img class="img-fluid" src="@item.ProductImage">
</a>
</div>
if (count == 2)
{
@:</div>
<div class="clearfix"></div>
}
}
else if (count > 2 && count <= 5)
{
<div class="col-sm-4">
<div class="highlights">
<a href="@Url.Action("tourdetail", "customer",new {key = QueryStringManager.Encrypt(item.ID)})">
<div class="highlithsTxt"> <p><strong>$@item.OnlinePrice</strong></p> <p>@item.ProductDisplayName</p> </div>
<img class="img-fluid" src="@item.ProductImage">
</a>
</div>
</div>
}
else if (count > 5 && count <= 8)
{
<div class="col-sm-4">
<div class="card activityBox activeBox">
<a href="@Url.Action("tourdetail","customer",new {key = QueryStringManager.Encrypt(item.ID)})">
<div class="dealTags">
<div class="tag-spcial">Special Offer</div>
</div>
<img class="card-img-top" src="@item.ProductImage" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">@item.ProductDisplayName</h5>
<div class="row">
<div class="col-md-4 dealPrice">
<label>From</label>
$@item.OnlinePrice <span>$30.00</span>
</div>
<div class="col-md-5 dealLocation"><label>Location</label>@item.DestinationName</div>
<div class="col-md-3 favIt"><i class="far fa-heart"></i></div>
</div>
</div>
</a>
</div>
</div>
}
else if (count > 8 && count < 12)
{
<div class="col-sm-4">
<div class="card activityBox">
<a href="@Url.Action("tourdetail","customer",new {key = QueryStringManager.Encrypt(item.ID)})">
<div class="dealTags">
<div class="tag-spcial">Special Offer</div>
</div>
<img class="card-img-top" src="@item.ProductImage" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">@item.ProductDisplayName</h5>
<div class="row">
<div class="col-md-4 dealPrice">
<label>From</label>
$@item.OnlinePrice <span>$30.00</span>
</div>
<div class="col-md-5 dealLocation"><label>Location</label>@item.DestinationName</div>
<div class="col-md-3 favIt"><i class="far fa-heart"></i></div>
</div>
</div>
</a>
</div>
</div>
}
count++;
}
if (ViewBag.productCount > 12)
{
skip += 12;
<div class="clearfix"></div>
<div class="loadMore"><a href="@Url.Action("destination","Home", new {key = key,searchingKey = searchingKey, skip = skip})"> Load More Results</a></div>
}
}
</div>
</div>
</div>
如果没有javascript和ajax,解决方案根本就不是跳过,而是增加你的项目数量。不使用ajax的“加载更多”似乎有点毫无意义。但您需要向方法传递一个值,该值指示要显示多少条记录,然后在查询中使用
.Take()
(no.Skip()
)并在if(productList.Count==0)中的else
语句中增加下一次调用的值
将永远不会为真,因为您每次都在创建一个新列表。@X3Vikas,您还必须将productList
传递给controller,就像传递skip
或searkingkey
而不是在视图中创建新实例一样。这样,在加载新数据时,您的早期数据应该保持不变
public ActionResult destination(string key, string searchingkey, FilterDataViewModel model, int skip = 0)
{
TempData["HeaderColor"] = true;
if (!string.IsNullOrEmpty(key))
{
int destinationId = QueryStringManager.Decrypt(key);
var destinationInfo = _masterRepo.GetMasterDataByID(destinationId);
var productList = _websiteRepo.GetAllProductListByDestinationId(destinationId).OrderByDescending(x => x.DestinationWeightPriority).ToList();
if (model.DestinationCityID > 0)
{
productList = (from prod in productList where prod.DestinationCity == model.DestinationCityID select prod).ToList();
}
ViewBag.productCount = productList.Count();
var a = productList.Skip(skip).Take(12).ToList();
ViewBag.ProductList = productList.Skip(skip).Take(12).ToList();
var allAdbyDestination = _websiteRepo.GetAllAdvertisementList(destinationId);
ViewBag.allDestinationAd = allAdbyDestination;
ViewBag.DestinationName = destinationInfo.MasterData;
ViewBag.DestinationImages = destinationInfo.ImagePath;
var categoryID = MasterConstant.GetCategoryID(CatergoryConstant.City);
var allDestinationCity = _masterRepo.GetMasterDataByCategoryIDandXrefID(categoryID, destinationId);
ViewBag.DestinationCityList = new SelectList(allDestinationCity, "ItemID", "MasterData");
ViewBag.Key = key;
ViewBag.searchingkey = searchingkey;
ViewBag.skip = skip;
return View(a);
}
else
{
return RedirectToAction("index", "home");
}
}