2016-04-12 28 views
0

我已經用數據庫中的圖片上傳了8個帶有圖片的產品,使用代碼第一個實體框架,並且所有的都在索引視圖中顯示,我只想顯示連續的前四個產品和後四個圖庫的第二個頁面,所以我該怎麼做。 ??這是我的代碼..我試圖使用@for循環,並告訴它前四個,但我沒有工作。如何在視圖中顯示前幾項?

public class Product  
{  
    [Key]  
    public int ProductId { get; set; }  
    [Display(Name ="Name: ")]  
    public string Name { get; set; }  
    [Display(Name ="Description: ")]  
    public string Description { get; set; }  
    [Display(Name = "Price: ")]  
    public decimal Price { get; set; }  
    [Display(Name ="Discount: ")]  
    public double Discount { get; set; }  
    [Display(Name ="Quantity: ")]  
    public int Quantity { get; set; }  
    public int ImageSize { get; set; }  
    public string FileName { get; set; }  
    public byte[] ImageData { get; set;}  
    [NotMapped]  
    [Required]  
    public HttpPostedFileBase File { get; set; }  
    public virtual Order Order { get; set; }  
}   
+0

發佈您目前嘗試過的循環代碼。您目前發佈的所有內容都是類定義。我們可以在視圖中使用 – CathalMF

回答

1

你也許可以使用Skip()Take()方法。 跳過將跳過您指定的記錄數,Take將採用您指定的下一個記錄數。您需要跟蹤已經顯示的記錄數量。

int AlreadyDisplayed = 0; 
int NumberToDisplay = 4; 

var Selections = context.Stuff 
        .Select() 
        .Skip(AlreadyDisplayed) 
        .Take(NumberToDisplay) 
        .ToList(); 

AlreadyDisplayed += NumberToDisplay; 
+0

嗎? –

+0

public ActionResult Index() { List all = new List ();使用(BootstrapDBContext db = new BootstrapDBContext()) { all = db.Product.ToList(); } return View(all); } –

+0

您必須從您的控制器進行查詢並在您的視圖中使用它。爲此,您可以使用ViewBag作爲例子。 –

相關問題