0

我正在寫一個商業應用程序,並且遇到了一個令人沮喪的錯誤,它不告訴我實際的錯誤是什麼。將購物車項目添加到數據庫時出錯

Error Message Details

這是一個正在運行

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult AddToCart(int product_ID) 
{ 
    if (db.Products.Any(product => product.Product_ID == product_ID)) 
    { 
     //var successfulAddToCart = false; 
     var cartItem = new Cart(); 

     // cartItem.CartID = Guid.NewGuid(); 

     if (Request.IsAuthenticated) 
     { 
      var Customer = db.Customers.FirstOrDefault(customer => customer.Email == User.Identity.Name); 
      cartItem.Customer = Customer; 
      cartItem.CustomerID = Customer.Customer_ID; 
     }      
     else//not logged in, need to remember them somehow 
     { 
      // add a cookie for a guest user so we can save their cart for some time. 
      var CartCookie = new HttpCookie("Cart", Guid.NewGuid().ToString()); 
      CartCookie.Expires.AddDays(2); 
      Request.Cookies.Add(CartCookie); 
      cartItem.Customer = new Customer(); 
      cartItem.CustomerID = null; 
     } 

     //obviously not checked out since we just added to cart 
     cartItem.IsCheckedOut = false; 
     cartItem.Quantity = 1; 
     //one to one with product, so grab the first sku you find. 
     var Sku = db.SKU_Table.SingleOrDefault(sku => sku.Product_ID == product_ID); 
     cartItem.Sku = Sku; 
     cartItem.SkuID = Sku.SKU_ID; 
     //if everything is okay, save the changes to the database 
     if (ModelState.IsValid) 
     { 
      db.Cart.Add(cartItem); 
      db.SaveChanges(); 
     } 


     //return a content string that displays success or failure 
     return Content("Add to cart was successful"); 
    } 
    return Content("Add to cart was not successful");//unsuccessful add 
} 

現在據我可以告訴代碼,我的模型是非常有效的,我不知道是什麼問題的來源是。如果這會有所幫助,我可以發佈購物車項目對象的值。錯誤消息並沒有真正的幫助,我沒有看到我的模型狀態有什麼問題,尤其是在它傳遞之後。

這裏是車模型

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 

namespace SeniorProjectMVC.Models 
{ 
    [Table("Cart")] 
    public class Cart 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ID { get; set; } 
     public Guid CartID { get; set; } 
     public int SkuID { get; set; } 
     public virtual Sku Sku { get; set; } 
     [Required] 
     public int Quantity { get; set; } 
     public int? CustomerID { get; set; } 
     public virtual Customer Customer { get; set; } 
     public bool IsCheckedOut { get; set; } 
    } 
} 
+0

* RequiredAttribute屬性指定當表單上的字段被驗證時,該字段必須包含一個值。如果屬性爲空,包含空字符串(「」)或僅包含空格字符,則會引發驗證異常。* ....請檢查您的'else'這行代碼中的內容'cartItem.CustomerID = null; '......改變它的類似'cartItem.CustomerID = 0;'看看它是否工作:) – Hackerman

+0

我試過,並且該字段不需要在數據庫中,因爲它是一個可爲空的整數。它在我的數據庫中作爲一個可空的int字段出來 – ddeamaral

+1

檢查'if(ModelState.IsValid)'是無意義的(你的模型是'int product_ID')並且它不需要設置'cartItem.Customer'和'cartItem.Sku'屬性(只是相關的'CustomerID'和'SkuID'屬性)。您沒有爲「CartID」設置一個值(您已註釋掉該行),因此會引發異常。 –

回答

0

我可以看到你所做的客戶ID模型爲空的,但也試着做你的數據庫的客戶ID字段可空,以及或允許客戶ID字段取空值。

+0

我已經完成了。我試着用0輸入它,並用null值發送它。在我的數據庫中,如果我展開我的購物車表格的列面板,它說我的CustomerID是一個FK類型爲int的外鍵。 – ddeamaral

+0

你可以試試這個,讓我告訴我最新的錯誤是什麼? http://mattrandle.me/viewing-entityvalidationerrors-in-visual-studio/ –