我有一个非常有趣的案例:在我的购物过程中的两个 View 中,我有一个显示订单总价和交货价格的部分 View 。
此部分 View 由 ShowCartDiscountsPrices
呈现行动。最后,我必须在数据库中使用 Entity Framework 插入 MinDeliveryPrice
, MinDeliveryPriceDDS
, DeliveryPrice
, DeliveryPriceDDS
.
奇怪的问题是,上周在我的 SQL Server 数据库中的两个案例中,我只有 DeliveryPrice
的值。不知何故,我的 SQL Server 数据库中的其他三列为空。
当我查看问题并使用相同的文章下订单时,例如一个小时后,一切都很好,填充了四列,在此期间没有人对数据库进行任何更改。
我想知道可能是什么问题我可以编写一些脚本来记录日志以查看 SQL Server 或 IIS 中是否发生某种错误?
public ActionResult ShowCartDiscountsPrices(OrderDiscountPriceModel model, bool? ischange, int? deliverypreference)
{
var cart = ShoppingCart.GetCart(WebsiteContext.CurrentUser != null ? (int?)WebsiteContext.CurrentUser.UserID : null);
decimal mindeliveryprice = 0;
decimal mindeliverypricedds = 0;
if (deliverypreference != null || Session["DeliveryPreference"] != null)
{
var parsedelprice = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPrice"), out mindeliveryprice) :Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPrice"), out mindeliveryprice) : Decimal.TryParse(Session["MinDeliveryPrice"].ToString(), out mindeliveryprice );
var parsedelpricedds = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Session["MinDeliveryPriceDDS"].ToString(), out mindeliverypricedds );
decimal deliveryprice;
decimal deliverypricedds;
ShoppingCartHelper.CalculateArticlesDeliveryPrice(mindeliveryprice, mindeliverypricedds, cart.ComputeTotalTransport(), cart.ComputeTotalTransportDDS(), out deliveryprice, out deliverypricedds);
model.DeliveryPrice = deliveryprice;
model.DeliveryPriceDDS = deliverypricedds;
model.DeliveryPreference = deliverypreference.HasValue ? deliverypreference.Value : (int)Session["DeliveryPreference"];
model.MinDeliveryPrice = mindeliveryprice;
model.MinDeliveryPriceDDS = mindeliverypricedds;
model.FinalSum += deliverypricedds;
}
else
{
if (isFromLastStep != null && bool.Parse(isFromLastStep.ToString()) == true)
ViewBag.IncludeDelivery = true;
}
if (deliverypreference != null)
{
Session["DeliveryPreference"] = deliverypreference;
Session["MinDeliveryPrice"] = mindeliveryprice;
Session["MinDeliveryPriceDDS"] = mindeliverypricedds;
}
ViewData.TemplateInfo.HtmlFieldPrefix = "OrderDiscoutPrice";
return PartialView("_CartDiscountsPrices", model);
}
这是我的辅助函数,它只遍历包含文章的集合并计算总交付价格。
public decimal ComputeTotalTransport()
{
decimal totalarticletransport = 0;
decimal articletransport;
foreach (var article in lineCollection)
{
if (decimal.TryParse(article.Article.Transport.ToString(), out articletransport))
{
totalarticletransport += article.Article.Quantity * articletransport;
articletransport = 0;
}
}
return totalarticletransport;
}
public decimal ComputeTotalTransportDDS()
{
decimal totaltransportdds = 0;
decimal articletransportdds;
foreach (var article in lineCollection)
{
if (decimal.TryParse(article.Article.TransportDDS.ToString(), out articletransportdds))
{
totaltransportdds += article.Article.Quantity * articletransportdds;
articletransportdds = 0;
}
}
return totaltransportdds;
}
最佳答案
我想建议您进一步扩展 if 表达式,如下所示,以检查 session 值是否大于 0
如下所示。
if (Convert.ToString(deliverypreference) != "" && Convert.ToString(Session["DeliveryPreference"]) != "" && Convert.ToDecimal(deliverypreference) > 0 && Convert.ToDecimal(Session["DeliveryPreference"]) > 0)
{
//Your insert logic as you have written.
}
您可能知道,有时您的 session 变量可能不是空白或空字符串,但 session 中的值等于 0。在这种情况下,您的代码将执行,但数据将无法正确插入。
我想建议您通过在设计模式下修改表来禁用 SQL Server 列中的允许空值的第二个解决方法。
关于sql-server - 未插入 SQL Server 数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53029689/