【发布时间】:2019-06-21 02:11:54
【问题描述】:
我是网络开发的新手,我正在为朋友的业务建立一个网站。我试图显示与公猪相关的详细信息,但每头公猪都有多个卖点(如示例图所示)。我正在努力获取视图的卖点。我已经成功地让 Boar 数据显示成功,但我想要每个 Boar 卖点的项目符号列表(如图所示)。
Boar.cs
public partial class Boar
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Boar()
{
SellingPoints = new HashSet<SellingPoint>();
}
[Key]
[Column(TypeName = "numeric")]
public decimal Boar_Id { get; set; }
[StringLength(255)]
public string Name { get; set; }
public DateTime? Farrowed { get; set; }
public byte? LitterSize { get; set; }
public byte? Price { get; set; }
public bool? GuaranteedSettle { get; set; }
[StringLength(255)]
public string StressTest { get; set; }
[StringLength(255)]
public string NameNoSpaces { get; set; }
[StringLength(255)]
public string Sire { get; set; }
[StringLength(255)]
public string SireFull { get; set; }
[StringLength(255)]
public string Dam { get; set; }
[StringLength(255)]
public string DamFull { get; set; }
[StringLength(255)]
public string Breed { get; set; }
public byte? Order { get; set; }
public bool? Featured { get; set; }
public short? FeaturedOrder { get; set; }
[StringLength(255)]
public string EarNotch { get; set; }
[StringLength(255)]
public string RegNum { get; set; }
[StringLength(255)]
public string TestData { get; set; }
[StringLength(255)]
public string BredBy { get; set; }
[StringLength(255)]
public string OwnedBy { get; set; }
[StringLength(255)]
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SellingPoint> SellingPoints { get; set; }
}
SellingPoint.cs
[Table("SellingPoint")]
public partial class SellingPoint
{
[Key]
[Column(TypeName = "numeric")]
public decimal SellingPoints_Id { get; set; }
[Column(TypeName = "numeric")]
public decimal? Boar_Id { get; set; }
public virtual Boar Boar { get; set; }
}
SellingPoints.cs
[Table("SellingPoints")]
public partial class SellingPoint1
{
[StringLength(255)]
public string SellingPoint { get; set; }
[Column(TypeName = "numeric")]
public decimal? SellingPoints_Id { get; set; }
public int ID { get; set; }
}
查询用例
SELECT Boars.Boar_Id, Boars.Name, Boars.Price, Boars.Breed, Boars.Sire, Boars.Dam, Boars.NameNoSpaces, SellingPoints.SellingPoint FROM Boars INNER JOIN SellingPoint ON Boars.Boar_Id = SellingPoint.Boar_Id JOIN SellingPoints ON SellingPoint.SellingPoints_Id = SellingPoints.SellingPoints_Id
Index.cshtml
@model IEnumerable<ShipleySwine.Boar>
@{
ViewBag.Title = "Index";
string imgURL;
int counter = 0;
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="container">
<div class="row row-centered">
@foreach (var item in Model)
{
imgURL = "../Boars/" + item.Breed + "/" + item.NameNoSpaces + "/Thumbs/" + item.NameNoSpaces + "1.jpg";
if (counter == 2)
{
@:</div>
@:<div class="row">
counter = 0;
}
<div class="col-lg-6">
<div class="card mb-3" style="max-width: 540px;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="@Url.Content(imgURL)" class="card-img" alt="Photo Didnt Load">
</div>
<div class="col-md-8">
<div class="card-body">
<div>
<div class="text-center">
<h4 class="card-title">@Html.DisplayFor(modelItem => item.Name)</h4>
</div>
</div>
<div class="text-center">
<h6 class="card-title">@Html.DisplayFor(modelItem => item.Sire) | @Html.DisplayFor(modelItem => item.Dam)</h6>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<h4>$ @Html.DisplayFor(modelItem => item.Price)</h4>
<p class="card-text"><small class="text-muted">@Html.DisplayFor(modelItem => item.SellingPoints)</small></p>
</div>
</div>
</div>
</div>
</div>
</div>
counter++;
}
</div>
【问题讨论】:
-
由于
SellingPoint是一个列表,因此您必须遍历集合并单独打印它们,或者您可以创建一个视图模型,其中包含每只野猪、SellingPonts 列表和其他内容。跨度> -
SellingPoints 表在物理上不需要,它是 Boar 模型中指定的虚拟集合。
标签: asp.net asp.net-mvc entity-framework