简体   繁体   English

在ASP.NET MVC中从数据库检索数据时遇到问题

[英]Having trouble in retrieving data from database in asp.net mvc

I have created 5 tables in my database: 我在数据库中创建了5个表:

  1. Categories 分类
  2. Product (FK, Category) 产品(FK,类别)
  3. Options 选项
  4. OptionGroups OptionGroups
  5. ProductOptions (FK, Options, OptionGroups, Products) 产品选项(FK,选项,选项组,产品)

In case of any confusion I have attached my ERD here .. 如有任何混淆,请在此处附加我的ERD。

I'm stuck at the scenario that I want a list of categories on my homepage. 我一直想在主页上找到类别列表。 When I select any one of them, it would show the option groups accordingly and in this option group, options will be shown. 当我选择其中任何一个时,它将相应地显示选项组,并且在该选项组中将显示选项。 How can I achieve this by using Entity Framework and LINQ queries? 如何通过使用实体框架和LINQ查询来实现这一点?

Please help me with the controller logic. 请帮助我控制器逻辑。 I have mapped the models as shown in the database. 我已经映射了数据库中显示的模型。 And also please help if I am making the database relation wrong? 如果我使数据库关系错误,也请帮忙?

Edit 编辑

I have got the list of OptionGroup by making a FK relation from Category to OptionGroups.. Now i want to get the list of each option group in the same action method.. Please help me with this linq query and then i want to retreive the product from that specific option.. please help me with that linq query too.. 我已经通过建立从类别到OptionGroups的FK关系获得了OptionGroup的列表。.现在,我想以相同的操作方法获取每个选项组的列表。.请使用此linq查询帮助我,然后我想重新学习该特定选项的产品。请也帮助我使用linq查询。

Controller 调节器

[HttpPost]
        public ActionResult GetSubCategories(int btnValue)
        {

            Entities entity = new Entities();
            HomeRoot root = new HomeRoot();
            root.OptionGroups = entity.OptionGroups.Where(m => m.CategoryID == btnValue).ToList();
            //Missing My logic Here 
            return View("SubCategories",root);

        }

Models 楷模

 public partial class ProductOption
    {
        public int ProductOptionID { get; set; }
        public int OptionID { get; set; }
        public int OptionGroupID { get; set; }
        public int ProductID { get; set; }
        public double OptionPriceIncrement { get; set; }

        public virtual OptionGroup OptionGroup { get; set; }
        public virtual Option Option { get; set; }
        public virtual Product Product { get; set; }
    }


 public partial class Product
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Product()
    {
        this.OrderDetails = new HashSet<OrderDetail>();
        this.ProductOptions = new HashSet<ProductOption>();
    }

    public int ProductID { get; set; }
    public string ProductSKU { get; set; }
    public string ProductName { get; set; }
    public Nullable<double> ProductPrice { get; set; }
    public Nullable<double> ProductWeight { get; set; }
    public string ProductCartDesc { get; set; }
    public string ProductShortDesc { get; set; }
    public string ProductLongDesc { get; set; }
    public string ProductThumb { get; set; }
    public string ProductImage { get; set; }
    public Nullable<int> CategoryID { get; set; }
    public byte[] ProductUpdateDate { get; set; }
    public Nullable<double> ProductStock { get; set; }
    public Nullable<byte> ProductLive { get; set; }
    public Nullable<byte> ProductUnlimited { get; set; }
    public string ProductLocation { get; set; }
    public string ProductColor { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ProductOption> ProductOptions { get; set; }
}

  public partial class OptionGroup
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public OptionGroup()
    {
        this.ProductOptions = new HashSet<ProductOption>();
    }

    public int OptionGroupID { get; set; }
    public string OptionGroupName { get; set; }
    public Nullable<int> CategoryID { get; set; }

    public virtual ProductCategory ProductCategory { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
public partial class Option
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Option()
    {
        this.ProductOptions = new HashSet<ProductOption>();
    }

    public int OptionID { get; set; }
    public string OptionName { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ProductOption> ProductOptions { get; set; }
}

1. 1。

In your case write like this to get selected category option groups list: 在您的情况下,请这样编写以获取所选类别选项组列表:

var SelectedCategoryOptionGroup= Context.ProductOptions
                .Where(po=>po.Product.CategoryID ==SelectedCategoryID)
                .Select(po=>po.OptionGroup)
                .Distinct();

Writes by Entityframework with Lazy loading technique. 通过Entityframework with Lazy loading技术Entityframework with Lazy loading SelectedCategoryID is your home page selected category id. SelectedCategoryID是您的主页选择类别ID。

2. 2。

Otherwise: If your OptionGroup direct related with Category , you must reference OptionGroup directly to Category. 否则:如果您的OptionGroupCategory直接相关,则必须将OptionGroup直接引用到Category。 in this case you more easy to achieve to target. 在这种情况下,您更容易实现目标。

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

相关问题 如何通过从 ASP.NET MVC 中的数据库检索数据来选中/取消选中复选框列表 - How to Check/Unchecked the check box list by retrieving the data from database in ASP.NET MVC 使用 ASP.NET MVC 从数据库中检索数据并插入到文本框 - Retrieving Data From Database and Inserting to Textbox Using ASP.NET MVC 在 ASP .NET Core 中从数据库获取图表数据时遇到问题 - Having trouble fetching data from database for chart in ASP .NET Core 从 ASP.NET Razor Pages 中的数据库检索数据 - Retrieving data from a database in ASP.NET Razor Pages Asp.net MVC遇到问题,将querystring作为参数传递 - Having trouble with Asp.net MVC passing in a querystring as a parameter 数据库(ASP.NET MVC)中缺少来自集合的数据 - Data from Collection is missing from database (ASP.NET MVC) 使用ASP.NET MVC Core将数据从数据库注入到类中 - Inject data from database to a class with ASP.NET MVC Core ASP.NET MVC应用程序从SQLite数据库读取数据 - ASP.NET MVC application reading data from SQLite database 在ASP.NET MVC 5中从数据库中获取字节数据中的图像 - Fetching images in byte data from database in asp.net mvc 5 ASP.NET MVC 5-从数据库选择数据的LINQ查询 - ASP.NET MVC 5 - LINQ Query to Select Data from Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM