Отображение атрибутов «один ко многим» в представлении - PullRequest
0 голосов
/ 21 июня 2019

Я новичок в веб-разработке, и я создаю сайт для бизнеса друзей. Я пытаюсь отобразить детали относительно Кабана, но у каждого кабана есть несколько точек продажи (показано на примере изображения). Я изо всех сил пытаюсь получить точки продаж для представления. Мне удалось успешно отобразить данные о кабане, но мне нужен маркированный список точек продажи для каждого кабана (как показано на рисунке).

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 ViewDatabase DesignMultiple SellingPoints

1 Ответ

0 голосов
/ 21 июня 2019

Вы можете использовать это для отображения SellingPoint в представлении

@foreach (var _it in item)
{
    <ul>
        <li><p class="card-text"><small class="text-muted">@_it.SellingPoint</small></p></li>
    </ul> 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...