繁体   English   中英

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

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

我有 4 个表,其中 3 个用于一个项目的类别和子类别。

类别

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; }
}

产品

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; }

}

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; }
}

项目.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; }
}

项目表示例:

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

现在我想在项目视图中显示表 Brand 中的名称。 我尝试使用 include 但它只能显示一个与视图相关的表。

这是我的代码:

控制器

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);
}

查看型号:

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

看法:

@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>

当我运行项目并浏览项目名称时,出现此错误:

System.NullReferenceException:未将对象引用设置为对象的实例。

您是否启用了 EagerLoading 并禁用了 LazyLoading? 表“项目”是数据库表还是只是一个普通的类?

我建议阅读 EagerLoading,它是如何工作的?

尝试这个:

控制器

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);
    }

编辑: - 我所做的解释都包括以确保导航属性不为空,然后根据您的视图模型分配它们。

我肯定会简化我的视图模型,因为它们可能会引起一些混乱,因为您的Item属性将具有导航属性。 IE:

genreModel.Item.Brand.BrandName将与genreModel.Brand.BrandName相同

暂无
暂无

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

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