asp.net - 生成一个类别的产品列表
问题描述
我正在开发一个商店应用程序,我需要展示每个类别的产品。问题是每个产品都是从存储在表中的产品模板创建的,并且每个模板都与一个类别相关。这是产品型号:
namespace fardashahr.Models
{
[Table("Product")]
public class ProductModel
{
public ProductModel()
{
if (Specs == null)
{
Specs = new Dictionary<string, SpecItemsModel>();
}
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int ProductTemplateId { get; set; }
[Required]
public bool IsPublished { get; set; }
[Required]
public bool InStock { get; set; }
[Range(95, 110)]
public float SyncRate { get; set; }
public DateTime? ProductionDate { get; set; }
[Required]
public DateTime RegisterationDate { get; set; }
public string ImageName { get; set; }
public IEnumerable<string> GalleryImages { get; set; }
[NotMapped]
public Dictionary<string, SpecItemsModel> Specs { get; set; }
[ForeignKey("ProductTemplateId")]
public virtual ProductTemplateModel ProductTemplate { get; set; }
[ForeignKey("ManufacturerId")]
public virtual CodingItemModel Manufacturer { get; set; }
[ForeignKey("BrandId")]
public virtual CodingItemModel Brand { get; set; }
[ForeignKey("ModelId")]
public virtual CodingItemModel Model { get; set; }
[ForeignKey("SeriesId")]
public virtual CodingItemModel Series { get; set; }
}
}
这是ProductTemplate:
namespace fardashahr.Models
{
[Table("ProductTemplate")]
public class ProductTemplateModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(500)]
public string Name { get; set; }
[Required]
public int CategoryId { get; set; }
[StringLength(500)]
public string Description { get; set; }
[ForeignKey("CategoryId")]
public virtual CategoryModel Category{ get; set; }
}
}
控制器是:
namespace fardashahr.Controllers
{
public class ProductsController : Controller
{
// GET: Products
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
public ActionResult Category(string name)
{
//declare a list of products
List<ProductModel> productList;
using(MainModel db = new MainModel())
{
//get category id
CategoryModel category = db.Categories.Where(x => x.CategorytUrl == name).FirstOrDefault();
int catId = category.Id;
//initialize the list
productList = db.Products.Where(x => x. == catId).ToList();
}
}
}
}
最后,我想知道的是如何初始化产品列表。
解决方案
在您的模型中,您添加virtual
了表示导航属性将自动加载而无需 LINQ lambda.include()
表达式的关键字。
因此,您可以立即访问导航属性并像这样加载列表;
productList = db.Products.Where(x => x.ProductTemplate.CategoryId == catId).ToList();
string categoryNameOfFirstProduct = productList.FirstOrDefault().ProductTemplate.Category.Name;
string categoryNameOfFirstProduct = productList.FirstOrDefault().ProductTemplate.Category.CategorytUrl;
推荐阅读
- java - 在另一个 jar 中升级易受攻击的 jar
- python - 在图中用数字替换列字符串
- google-bigquery - bigquery 中是否有任何函数可以将 IST 或 PST 或任何本地时区转换为 UTC
- c# - 如何使用 C# 将 powershell 脚本编码为 base64 UTF16-LE 字符串
- html - 为什么悬停不起作用
- 标签按钮?
- node.js - 不能将我的 Index.js 与 Mongodb 连接,我如何连接它们以使用 node.js 创建 API?
- regex - 从正则表达式中的第二个标识符而不是第一个标识符中提取数据
- python - 当'else'应该在python基本猜测操作中激活时,如何修复'if'语句激活'and'参数?
- excel - 比较两个 Excel 文件并将任何不存在的条目从第一个复制到第二个
- plot - 绘制未来日期