首页 > 解决方案 > 在视图中显示一对多属性

问题描述

我是网络开发的新手,我正在为朋友的业务建立一个网站。我试图显示与公猪相关的详细信息,但每头公猪都有多个卖点(如示例图所示)。我正在努力获取视图的卖点。我已经成功地让 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; }
}

卖点.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; }
}

卖点.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

索引.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>

最终视图数据库设计多个卖点

标签: asp.netasp.net-mvcentity-framework

解决方案


您可以使用它SellingPoint在视图中显示

@foreach (var _it in item)
{
    <ul>
        <li><p class="card-text"><small class="text-muted">@_it.SellingPoint</small></p></li>
    </ul> 
}

推荐阅读