繁体   English   中英

多个价格的 ASP.NET Core MVC 产品数据库

[英]ASP.NET Core MVC Product Database with Multiple Prices

我正在将我的网站之一从 WebForms 升级到 MVC Core。 你能给我一些关于如何创建他们的数据库的建议吗? 以下是Rabbit Bikes Products页面后面的 .VB 文件中的一些示例代码。 这是非常混乱的,因为他们的很多产品都有两个或更多的价格。

For Each prod As Product In myProducts
        'Beginning Layout
        temp &= "<article class=""dbItem""><figure class=""left dbImage"">"

        temp &= "<img src=""" & prod.ImagePath & """ alt=""" & prod.Name & """ title=""" & prod.Name & """ style=""width: 100%;"" class=""pull-left img-rounded""></figure>"
        temp &= "<h2>" & prod.Name & "</h2>"
        temp &= "<div class="" scroller""><p>" & prod.Description & "</p></div>"
        If Not prod.Description.Contains("shuttle") And Not prod.Name = "Trail Pass" Then
            temp &= "<h3 style=""text-transform: uppercase;"">Call to reserve yours today!</h3>"
        Else
            temp &= "<h3 style=""text-transform: uppercase;"">Call to purchase one today!</h3>"
        End If
        temp &= "<br /><div style=""text-align: center;"">"
        If prod.Description.Contains("shuttle") Or prod.Description.Contains("frequent rider") Then
            temp &= "<span class=""oSnap"">One Person:</span> " & prod.TwoHrPrice.ToString("c") & "<br/>"
            temp &= "<span class=""oSnap"">2 or More <abbr title=""people"">ppl.</abbr>:</span> " & prod.FourHrPrice.ToString("c") & "</p>"
        End If
        If prod.TwoHrPrice = 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<p>Free with purchase!</p>"
        End If
        If prod.FourHrPrice <> 0 And Not prod.Description.Contains("shuttle") And Not prod.Description.Contains("frequent rider") Then
            temp &= "<p><span class=""oSnap"">Half Day Price:</span> " & prod.FourHrPrice.ToString("c") & "<br/>"
        End If
        If prod.OneDayPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">One Day Price:</span> " & prod.OneDayPrice.ToString("c") & "<br/>"
        End If
        If prod.TwoDayPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">Two Day Price:</span> " & prod.TwoDayPrice.ToString("c") & "<br/>"
        End If
        If prod.WeekPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">Week Price:</span> " & prod.WeekPrice.ToString("c") & "</p>"
        End If

        'End Layout
        temp &= "</div></article>"
    Next

自从切换到 ASP.NET Core 1.1 和实体框架后,我一直在尝试弄清楚如何检查每个项目的价格数量并适当地标记它们。 我尝试使用数组,但它们不起作用,因为实体框架模型类中的每个变量类型都连接到 SQL Server 数据类型。 我不想使用一堆 if/else 语句来计算与每个产品相关联的价格并再次适当地标记它们。 我的产品类与带有实体框架和 CSS 的 ASP.NET MVC 中的产品类没有什么不同。

public class Product
{
    public int ID { get; set; }
    public string Image { get; set; }
    [Display(Name = "Product Name")]
    public string Name { get; set; }
    public string Description { get; set; }
    public string PriceLabels { get; set; }
    public decimal Prices { get; set; }
    //Add a third table to hold price information?
    //public int? PriceTableID { get; set; }
    public int? CategoryID { get; set; }
    [Display(Name = "Category Name")]
    public virtual Category Category { get; set; }
}

如果您对这个问题有任何见解,我将不胜感激。

你的问题是什么? 通用的“建议”在这里是题外话。 你应该问一个特定的问题以获得特定的答案。 如果您的问题是如何计算价格,您只需进行查询并使用计数方法 linq。 例如,假设您的数据库上下文称为 db,而您的产品表是 Products。

var count = db.Products.Where(x => x.ProductId == yourproductid).Count();

在这种情况下,每个产品/价格组合都有多个条目。 另一种选择是创建一对多的产品价格,然后您将分别查询价格。

这确实是Linq 的基本功能,而且你似乎非常缺乏经验,所以我建议阅读一些有关实体框架的教程。 EntityFramework 核心在细节上有很大不同,但就查询而言,其工作方式与传统实体框架大致相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM