gpt4 book ai didi

c# - 加载一对多模型

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:51 24 4
gpt4 key购买 nike

我有这样的模型:

public partial class Goods
{
public decimal good_id { get; set; }
public string good_name { get; set; }
public virtual ICollection<Description> description { get; set; }
}

public partial class Description
{
public decimal des_id { get; set; }
public decimal des_good_id { get; set; }
public string des_name { get; set; }
public string des_value { get; set; }
public virtual Goods goods { get; set; }
}

对于 Goods 中的每种商品,我如何从描述模型中加载与商品具有相同 ID 的所有数据?

我的 Controller :

public ActionResult Index()
{
var parts = _db.Goods.Include(p => p.Details);
return View(parts.ToList());
}

这东西不工作:

@foreach (var part in Model)
{
<div class="well">
<div class="row">
<div class="col-xs-10">
<h3>
<strong>@part.good_name</strong>
</h3>
<h4>
<strong>@part.Description</strong>
</h4>
</div>
</div>
</div>
}

最佳答案

据我了解你的问题,你有模型,你的模型中也有一个列表,你想用列表检索数据

Here is your controller code

public ActionResult Index()
{
var parts = _db.Goods.Include("Descriptions").Where(x=>x.good_id == id);
return View(parts));
}

其中“id”是基于您的数据将被找到的 id

Here is your View code with razor engine

 @import YourApplicationName.Model.Goods
@foreach (var part in @Model.Description)
{
<div class="well">
<div class="row">
<div class="col-xs-10">
<h3>
<strong>@part.des_name</strong>
</h3>
<h4>
<strong>@part.des_value</strong>
</h4>
</div>
</div>
</div>
}

Hope this is your answer

关于c# - 加载一对多模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43909634/

24 4 0
文章推荐: python - 在 Python 中匹配 "Chinese+Number"模式的正则表达式
文章推荐: php -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com