2012-11-28 46 views
0

我很確定我在這段代碼的正確路線上,但是我想要做的是在頁面上顯示特定的記錄,並使用導航控件允許您前往到下一個和上一個記錄(MVC3中生成的詳細信息視圖頁面的修改版本)。在詳細信息頁面上記錄導航

當我導航到頁面時,代碼通過ViewBag變量初始化ActionLink按鈕,並在相應控制器中的此方法中設置。

我的問題是,有沒有更好的方法去做下面的事情,同時防止出問題的數據庫記錄的問題?

public ViewResult Details(int id) 
{ 
    //Conditional Statements to manage navigation controls 
    if (db.tblQuoteLog.OrderByDescending(x => x.LogDate).Any(x => x.nID < id)) 
    { 
     //Set value next button 
     ViewBag.NextID = id; 

     ViewBag.PreviousID = db.tblQuoteLog.OrderByDescending(x => x.LogDate).FirstOrDefault(x => x.nID > id).nID; //Inverted logic due to orderby 
    } 
    else if (db.tblQuoteLog.OrderByDescending(x => x.LogDate).Any(x => x.nID > id)) 
    { 
     ViewBag.NextID = db.tblQuoteLog.OrderByDescending(x => x.LogDate).FirstOrDefault(x => x.nID < id).nID; //Inverted logic due to orderby 

     //Set value previous button 
     ViewBag.PreviousID = id; 
    } 
    else 
    { 
     //Set value next button 
     ViewBag.NextID = db.tblQuoteLog.OrderByDescending(x => x.LogDate).FirstOrDefault(x => x.nID < id).nID; 

     //Set value previous button 
     ViewBag.PreviousID = db.tblQuoteLog.OrderByDescending(x => x.LogDate).FirstOrDefault(x => x.nID > id).nID; 
    } 

    tblQuoteLog tblquotelog = db.tblQuoteLog.Find(id); 

    return View(db.tblQuoteLog.Where(x => x.nID == id).FirstOrDefault()); 
} 

編輯 我做了一個改變我的邏輯,這似乎從工作的想法邁克給了罰款(可能不整潔,但它更小)。

 //EOF is set to true if no records are found. 
     var nextRecord = (from r in db.tblQuoteLog 
          orderby r.Quote_ID descending 
          where r.Quote_ID < id 
          select new 
          { 
           Quote_ID = r.Quote_ID, 
           EOF = false 
          }).Take(1). 
          FirstOrDefault() ?? new { Quote_ID = id, EOF = true }; 

     var previousRecord = (from r in db.tblQuoteLog 
           orderby r.Quote_ID ascending 
           where r.Quote_ID > id 
           select new 
           { 
            Quote_ID = r.Quote_ID, 
            EOF = false 
           }).Take(1). 
           FirstOrDefault() ?? new { Quote_ID = id, EOF = true }; 

     //Conditional Statements to manage navigation controls 
     if ((nextRecord.EOF == true)) 
     { 
      //Set value next button 
      ViewBag.NextID = id; 

      ViewBag.PreviousID = previousRecord.Quote_ID; 
     } 
     else if ((previousRecord.EOF == true)) 
     { 
      ViewBag.NextID = nextRecord.Quote_ID; 

      //Set value previous button 
      ViewBag.PreviousID = id; 
     } 
     else 
     { 
      //Set value next button 
      ViewBag.NextID = nextRecord.Quote_ID; 

      //Set value previous button 
      ViewBag.PreviousID = previousRecord.Quote_ID; 
     } 

錯誤檢查現在使用匿名類型在Linq查詢中進行。我使用EOF(文件結束)標誌,以便在找不到記錄時將ID設置爲當前記錄,並將EOF設置爲true。

感謝您的建議傢伙:)。

+0

不管什麼方案,我建議你配發的移動是邏輯的服務類之後,以便您的行動方法是薄。 – Mike

回答

0

那麼從id-1中選擇前三位怎麼樣;

public ViewResult Details(int id) 
{ 
    var items = db.tblQuoteLog.OrderByDescending(x => x.LogDate).Where(x => x.Id >= (id - 1)).Take(3); 
} 
  • 是否有結果3個項目,你有你上一頁,下一頁和電流
  • 如果有結果2項,你的id是
  • 最後一頁
  • 如果有1名或0的項目,你的id是無效

可能需要一些更多的思考(例如,如果你的ID < 2),但其潛在的路徑

0

我認爲這是一個相當不錯的挑戰,所以我打開筆記本電腦,並給它一個去。

我走了我的第一個答案的路線,但實際上爲了有2個查詢而不是3它產生了蹩腳的代碼分配。所以我簡化了它。如果您在創建的Id列上都設置索引,則此操作應該很快。

PageService是您要查看的類。

class Program 
{ 
    static void Main(string[] args) 
    { 
     Database.SetInitializer<MyDbContext>(null); 

     var context = new MyDbContext(@"Data Source=.;Initial Catalog=Play;Integrated Security=True;"); 

     PageService service = new PageService(context); 
     while (true) 
     { 
      Console.WriteLine("Please enter a page id: "); 
      var pageId = Console.ReadLine(); 

      var detail = service.GetNavigationFor(Int32.Parse(pageId)); 

      if (detail.HasPreviousPage()) 
      { 
       Console.WriteLine(@"Previous page ({0}) {1} {2}", detail.PreviousPage.Id, detail.PreviousPage.Name, detail.PreviousPage.Created); 
      } 
      else 
      { 
       Console.WriteLine(@"No previous page"); 
      } 

      Console.WriteLine(@"Current page ({0}) {1} {2}", detail.CurrentPage.Id, detail.CurrentPage.Name, detail.CurrentPage.Created); 


      if (detail.HasNextPage()) 
      { 
       Console.WriteLine(@"Next page ({0}) {1} {2}", detail.NextPage.Id, detail.NextPage.Name, detail.NextPage.Created); 
      } 
      else 
      { 
       Console.WriteLine(@"No next page"); 
      } 

      Console.WriteLine(""); 
     } 


    } 
} 

public class PageService 
{ 
    public MyDbContext _context; 

    public PageService(MyDbContext context) 
    { 
     _context = context; 
    } 

    public NavigationDetails GetNavigationFor(int pageId) 
    { 
     var previousPage = _context.Pages.OrderByDescending(p => p.Created).Where(p => p.Id < pageId).FirstOrDefault(); 
     var nextPage = _context.Pages.OrderBy(p => p.Created).Where(p => p.Id > pageId).FirstOrDefault(); 
     var currentPage = _context.Pages.FirstOrDefault(p => p.Id == pageId); 

     return new NavigationDetails() 
     { 
      PreviousPage = previousPage, 
      NextPage = nextPage, 
      CurrentPage = currentPage 
     }; 
    } 
} 

public class NavigationDetails 
{ 
    public Page PreviousPage { get; set; } 
    public Page CurrentPage { get; set; } 
    public Page NextPage { get; set; } 

    public bool HasPreviousPage() 
    { 
     return (PreviousPage != null); 
    } 

    public bool HasNextPage() 
    { 
     return (NextPage != null); 
    } 
} 

public class MyDbContext : DbContext 
{ 
    public MyDbContext(string nameOrConnectionString) 
     : base(nameOrConnectionString) 
    { 
    } 

    public DbSet<Page> Pages { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new PageMap()); 
    } 

} 

public class PageMap : EntityTypeConfiguration<Page> 
{ 

    public PageMap() 
    { 
     ToTable("t_Pages"); 

     Property(m => m.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     Property(m => m.Name); 
     Property(m => m.Created); 
    } 

} 

public class Page 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public DateTime Created { get; set; } 
} 

}

SQL代碼

USE [Play] 
GO 

/****** Object: Table [dbo].[t_Pages] Script Date: 11/28/2012 20:49:34 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[t_Pages](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](50) NOT NULL, 
    [Created] [datetime] NULL, 
CONSTRAINT [PK_t_Page] PRIMARY KEY CLUSTERED 
(
    [Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
+0

這是一個有趣的方式去做,謝謝你的建議:)。 – Tay

相關問題