简体   繁体   English

如何使用 LinQ 在视图 ASP.NET MVC 中显示 2 个外键

[英]How do I display 2 foreign keys in a view ASP.NET MVC by using LinQ

I have 4 tables, 3 of them are used for category and sub category for one item.我有 4 个表,其中 3 个用于一个项目的类别和子类别。

Category类别

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Slug { get; set; }
    public string Body { get; set; }


    public ICollection<Item> Items { get; set; }

    public ICollection<Product> Products { get; set; }
}

Product产品

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }

    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }

    //Product can have many items
    public ICollection<Item> Items { get; set; }

    //Product can have many brands
    public ICollection<Brand> Brands { get; set; }

}

Brand

public class Brand
{
    public int BrandId { get; set; }
    public string BrandName { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }

    //Brand can have many items.
    public ICollection<Item> Items { get; set; }
}

Item.cs项目.cs

public class Item
{
    public int ItemId { get; set; }

    [Required]
    [StringLength(255, MinimumLength = 3)]
    [Display(Name = "Item's Name")]
    public string Name { get; set; }

    public string ImagePath { get; set; }

    [AllowHtml]
    public string Description { get; set; }

    [Required]
    public decimal Price { get; set; }

    public DateTime CreatedOn { get; set; }
    public bool IsActived { get; set; }
    public bool IsDeleted { get; set; }

    public bool IsDiscounted { get; set; }

    [Required]
    [Display(Name = "Category")]
    public int CategoryId { get; set; }  
    public Category Category { get; set; }

    [Required]
    [Display(Name = "Product")]
    public int ProductId { get; set; }
    public Product Product { get; set; }

    [Required]
    [Display(Name = "Brand")]
    public int BrandId { get; set; }
    public Brand Brand { get; set; }
}

Example for the item table:项目表示例:

id      name            categoryId   productId   BrandId
---------------------------------------------------------
1       Google Phone        1            1          1

Now I want to display name from table Brand in item view.现在我想在项目视图中显示表 Brand 中的名称。 I have tried to use include but it can only display one related table to the view.我尝试使用 include 但它只能显示一个与视图相关的表。

Here is my code:这是我的代码:

Controller控制器

public ActionResult Browse(string name)
{
        var genreModel = db.Products.Include("Items")
            .SingleOrDefault(g => g.Name == name);
        var viewModel = new BrowseViewModel
        {
            Product = genreModel
        };

        return View(viewModel);
}

View model:查看型号:

public class BrowseViewModel
{
    public Product Product { get; set; }
    public Item Item { get; set; }
    public Brand Brand { get; set; }
}

View:看法:

@model eStore.ViewModels.BrowseViewModel
@foreach (var item in Model.Product.Items)

<li><img src="/Images/Uploads/items/@item.ItemId/Thumbs/@item.ImagePath" class="img-responsive"></li>
<li>@Html.ActionLink(item.Name, "Detail", "Shop", new { id = item.ItemId }, null)</li>
<li>@item.Price</li>
<li>@item.Brand.BrandName</li>

When I run the project, and browse on item name, I get this error:当我运行项目并浏览项目名称时,出现此错误:

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:未将对象引用设置为对象的实例。

Did you enable EagerLoading and Disabled LazyLoading?您是否启用了 EagerLoading 并禁用了 LazyLoading? Table "Item" is Database table or just a plain class ?表“项目”是数据库表还是只是一个普通的类?

I would suggest read about EagerLoading, how does it work?我建议阅读 EagerLoading,它是如何工作的?

try this:尝试这个:

Controller控制器

public ActionResult Browse(string name)
    {
        var genreModel = db.Items.Where(x=> x.Name == name).Include(x=>x.Product).Include(x=>x.Brand).FirstOrDefault();
        var viewModel = new BrowseViewModel
        {
            Product = genreModel.Product,
            Item = genreModel ,
            Brand = genreModel.Brand
        };


        return View(viewModel);
    }

Edit: - explanation I do both includes to to ensure the navigational properties are not null, then assign them as per your viewmodel.编辑: - 我所做的解释都包括以确保导航属性不为空,然后根据您的视图模型分配它们。

I would definitely simplify my view model as they may cause some confusion as your Item property will have the navigational properties present.我肯定会简化我的视图模型,因为它们可能会引起一些混乱,因为您的Item属性将具有导航属性。 ie: IE:

genreModel.Item.Brand.BrandName will be the same as genreModel.Brand.BrandName genreModel.Item.Brand.BrandName将与genreModel.Brand.BrandName相同

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

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