我的项目是抛出上述错误,因为我表中的发货日期为空。我使用的是 Code First,并且 DateTime 字段在泛型集和 SQL 表中都被声明为可为空。我正在使用结果集填充网格视图。理想情况下,当 ShipDate 字段为空时,我希望 gridview 显示“尚未发货”,但此时,我很高兴能够显示记录。任何帮助是极大的赞赏。这是我正在使用的代码隐藏和上下文类:
public class Order
{
[Key]
public int OrderID { get; set; }
public int CustomerID { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipState { get; set; }
public string ShipZip { get; set; }
[ScaffoldColumn(false)]
public string PaymentTransactionID { get; set; }
[ScaffoldColumn(false)]
public System.DateTime OrderDate { get; set; }
public System.DateTime? ShipDate { get; set; }
public virtual Customers Customers { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
后面的代码是:
public List<OrderDetail> FetchByOrderID()
{
int myOrderID = CusOrderId;
ProductContext db = new ProductContext();
return db.OrderDetails.Where(
c => c.OrderID == myOrderID).ToList();
}