gpt4 book ai didi

c# - ASP.NET MVC3 和 Entity Framework - 一个 View 中的一对多关系

转载 作者:行者123 更新时间:2023-11-30 12:14:48 26 4
gpt4 key购买 nike

我正在学习 MVC 3,但我遇到了一件事。

我的模型中有两个表(图库和图像)。这些表与 GalleryId 相关(一对多)(1 个画廊 - 许多图像)。

在图库 -> 详细信息 View 中,我想插入图库详细信息(这很简单)和一件事 - 来自该图库的图像列表。我不知道该怎么做。以下是此模型中的类:

public partial class Gallery
{
public Gallery()
{
this.Images = new HashSet<Image>();
}

public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public System.DateTime CreatedOn { get; set; }

public virtual ICollection<Image> Images { get; set; }
}
public partial class Image
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public int GalleryId { get; set; }
public System.DateTime UploadedOn { get; set; }

public virtual Gallery Gallery { get; set; }
}
public partial class MyEntities : DbContext
{

public DbSet<Gallery> Galleries { get; set; }
public DbSet<Image> Images { get; set; }
}

最好的方法是什么?

最佳答案

在详细 View 中,您使用图库作为模型(在 Controller 中您返回所需的图库 return ActionResult View(service.GetGallery(id));

在 View 中你有一个循环:

@foreach (var item in Model.Images.Select((model, index) => new { index, model }))
{
<div>@(item.index). @item.model.Name </div>
}

关于c# - ASP.NET MVC3 和 Entity Framework - 一个 View 中的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8880274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com