在视图中显示一对多属性

时间:2019-06-21 02:11:54

标签: asp.net asp.net-mvc entity-framework

我是网络开发的新手,我正在为朋友业务建立一个网站。我试图显示有关公猪的详细信息,但每个公猪都有多个卖点(如示例图片所示)。我正在努力把卖点弄清楚。我设法使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>

Final View Database Design Multiple SellingPoints

1 个答案:

答案 0 :(得分:0)

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

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