2012-11-10 57 views
1

我下面演示網編輯直列 它的工作原理,但不會改變數據庫,它只是改變popolated 什麼,我需要做的下列表劍道UI網 - 在線編輯不會更改數據庫

這裏的代碼

public static IList<Book> All() 
    { 
     IList<Book> result = (IList<Book>)HttpContext.Current.Session["Books"]; 

     if (result == null) 
     { 
      HttpContext.Current.Session["Books"] = result = 
       (from book in new LibraryModel().Books 
       select new Book 
       { 
        PKiBookID=book.PKiBookID, 
        SBookName = book.SBookName, 
        SBookWriter= book.SBookWriter, 
        SDescription=book.SDescription, 
        BStatus=book.BStatus, 
        FKiBookType=book.FKiBookType 

       }).ToList(); 
     } 

     return result; 
    } 
    public static Book One(Func<Book, bool> predicate) 
    { 
     return All().Where(predicate).FirstOrDefault(); 
    } 
    public static void Insert(Book book) 
    { 
     //book.PKiBookID = All().OrderByDescending(p => p.PKiBookID).First().PKiBookID + 1; 
     All().Insert(0,book); 
    } 
    public static void Update(Book book) 
    { 
     Book target = One(p => p.PKiBookID == book.PKiBookID); 
     if (target != null) 
     { 
      target.SBookName = book.SBookName; 
      target.SBookWriter = book.SBookWriter; 
      target.SDescription = book.SDescription; 
      target.BStatus = book.BStatus; 
      target.BookType = new LibraryModel().BookTypes.Single(s => s.PKiBookTypeID == book.FKiBookType); 
     } 
    } 
+0

我使用螢火蟲,發現錯誤是'串行化'LibraryProject.Book''類型的對象時檢測到循環引用任何關於搜索的建議將有所幫助。課本由openaccess orm(數據庫優先)與asp.net MVC4生成。之後,它仍然插入到數據庫,但我要重建整個解決方案 – user1392853

回答

0

這個例外是quite well known,有很多文章如何避免它。它也覆蓋在Documentation

+0

我插入帶有引用密鑰的記錄後,第一次加載完成後,我現在可以執行的操作出現此錯誤 – user1392853

+0

我使用函數將數據綁定到在網格中的下拉菜單來選擇父類別'私人無效populateBook() { 計算機[ 「的BookType」] =新LibraryModel()。BookTypes 。選擇(E =>新BookTypeViewModel { PKiBookTypeID = e.PKiBookTypeID, sBookTypeName = e.SBookTypeName }); ();我在Index之前調用了這個函數'public ActionResult Index() { populateBook(); return View(); }' – user1392853