gpt4 book ai didi

mysql - ASP.Net MVC Entity Framework 和关系

转载 作者:行者123 更新时间:2023-11-30 22:42:53 25 4
gpt4 key购买 nike

我正在努力了解 ASP.Net MVC,因为我有 Web 窗体背景。

我有一个包含 2 个实体的 MySQL 数据库:

artwork - id(PK), artistid(FK), name...
artist - id(PK), artist, website...

我使用这个数据库和 ArtistController.cs 和 ArtworkController.cs 创建了一个 ADO.Net 实体数据模型,后者创建了相应的 View 。

现在当我访问 http://localhost:49664/Artwork 时,该页面显示 artwork 表中所有行的列表,但是,它显示 artistid。我该如何改为显示艺术家的名字?

更新:

在数据库中,艺术家表中的字段名为“艺术家”,但在创建实体数据模型时,Visual Studio 将其命名为“艺术家 1”。

我试过“@Html.DisplayFor(modelItem => item.artist.artist)”,但仍然显示艺术家 ID。

美术类:

public partial class artwork
{
public int id { get; set; }
public Nullable<int> artistId { get; set; }
public string title { get; set; }
public Nullable<int> editionId { get; set; }

public virtual artist artist { get; set; }
public virtual edition edition { get; set; }
}

艺术家类:

public partial class artist
{
public artist()
{
this.artworks = new HashSet<artwork>();
}

public int id { get; set; }
public string artist1 { get; set; }
public string website { get; set; }

public virtual ICollection<artwork> artworks { get; set; }
}

艺术作品 Controller :

public ActionResult Index()
{
var artworks = db.artworks.Include(a => a.artist).Include(a => a.edition);
return View(artworks.ToList());
}

作品查看代码:

@model IEnumerable<myflatfile.artwork>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.artist1)
</th>
<th>
@Html.DisplayNameFor(model => model.title)
</th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.artist.artist1)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
</tr>
}

</table>

最佳答案

我用记事本给你准备了一些东西,我有点困了。所以,如果有错误,请再次写信给我。

美术类:

public class artworklist
{
public List<artwork> ArtworkModelList { get; set; }
}


public partial class artwork
{
public int id { get; set; }
public Nullable<int> artistId { get; set; }
public string title { get; set; }
public Nullable<int> editionId { get; set; }

public virtual artist artist { get; set; }
public virtual edition edition { get; set; }
}

艺术家类:

public class artistlist
{
public List<artist> ArtistModelList { get; set; }
}

public partial class artist
{
public artist()
{
this.artworks = new HashSet<artwork>();
}

public int id { get; set; }
public string artist1 { get; set; }
public string website { get; set; }

public virtual ICollection<artwork> artworks { get; set; }
}

查看:

@model Art.Model.ArtistList



@if (null == Model || null == Model.ArtistModelList || 0 == Model.ArtistModelList.Count)
{
<tr>
<td>There is no data.</td>
</tr>
}
else
{
foreach (var art in Model.ArtistModelList)
{
<tr>
<td>@@@art.artist</td>
</tr>
}
}

Controller :

 [HttpGet]
public ActionResult Art()
{
var model = new ArtistList();

using (var dbContext = new ArtContext())
{
var artists = (from m in dbContext.Artists
select new ArtistModel
{
ID = m.ID,
Artist1 = m.Artist1,
Website = m.Website,
Artworks = m.Artworks
}).ToList();

model.ArtistModelList = artists;
return View(model);
}

[HttpPost]
public ActionResult Art(Artist model)
{

Artist art = new Artist();

art.ID = model.ID,
art.Artist1 = model.Artist1,
art.Website = model.Website,
art.Artworks = model.Artworks

using (ArtContext db = new ArtContext())
{

db.Artists.Add(art);
db.SaveChanges();

return RedirectToAction("*****", "*****"); // where you want (action,url)
}

}

关于mysql - ASP.Net MVC Entity Framework 和关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628585/

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