c# - 我想为网站开发 CRUD 界面,但在 Edit 方法中不断收到错误

标签 c# html asp.net

我得到的错误是:

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Albums_dbo.Artists_ArtistId". The conflict occurred in database "MusicStoreData", table "dbo.Artists", column 'ArtistId'

它发生在 HttpPost 编辑操作中。

我想做的只是编辑一个特定的专辑。例如:有一张名为 AC/DC 的专辑,我希望能够编辑流派、标题、艺术家等。

我尝试更改 View 并在数据库中放置包含当前流派和艺术家的下拉列表,但随后出现引用完整性错误。

这是 Controller :

public ActionResult Edit(int id)
{
        Album album = db.Albums.Find(id);

        return View(album);
}

[HttpPost]
public ActionResult Edit(Album album)
{
        if(ModelState.IsValid)
        {
            //Album editAlbum = db.Albums.Find(album.AlbumId);
            //album.AlbumId = editAlbum.AlbumId;
            db.Entry(album).State = EntityState.Modified;
            db.SaveChanges();
            return (RedirectToAction("Index"));
        }

        return View(album);
}

这是 View :

@model MvcMusicStore.Models.Album

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (@Html.BeginForm())
{
    <fieldset>
        @Html.HiddenFor(x => x.AlbumId)
        <legend>Album</legend>

        <div>
            Genre<br />
            @Html.TextBoxFor(x => x.Genre.Name)
        </div>
        <div>
            Artist<br />
            @Html.TextBoxFor(x => x.Artist.Name)
        </div>
        <div>
            Title<br />
            @Html.EditorFor(x => x.Title)
        </div>
        <div>
            Price<br />
            @Html.TextBoxFor(x => x.Price)
        </div>
        <div>
            Description<br />
            @Html.EditorFor(x => x.Genre.Description)
        </div>

        <p><input type="submit" value="Save" /></p>
    </fieldset>
}

<div>@Html.ActionLink("Return to List", "Index")</div>

这些是模型类:

namespace MvcMusicStore.Models
{
    public class Album
    {
        public int AlbumId { get; set; }
        public int GenreId { get; set; }
        public int ArtistId { get; set; }
        public virtual Genre Genre { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public virtual Artist Artist { get; set; }
    }

    public class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Album> Albums { get; set; }
    }

    public class Artist
    {
        public int ArtistId { get; set; }
        public string Name { get; set; }
    }
}

和 DbContext:

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Artist> Artists { get; set; }
    }
}

我知道我错过了一些东西,但我真的无法指出它。如果有人愿意帮助我,我会很高兴。谢谢!

最佳答案

您可以通过两种方式解决这个问题:

<强>1。简单方法和不完整方法 - 只是为了消除该错误

只需添加:

 @Html.HiddenFor(x => x.ArtistId)

到您的表格。您的请求是无状态的,因此服务器仅发送表单中包含的内容。然而,这将使您无法使用其中一个文本框更改您的艺术家(但这也不起作用,我将在第二个选项中触及)。

<强>2。稍微好一点的方法

现在,您正在将与您的模型不匹配的数据发布到 Controller 。您的 Controller 需要一个 Album 类型对象,而您正在发布诸如 Genre.Name 之类的内容,而 Album 没有。因此,您的 Controller 将默认为这些对象的外键为 0 (这就是您现在面临的问题)。我建议创建一个名为 AlbumEditViewModel 的新类,其中包含 GenreNameArtistName 的文本字段(您可能想要删除 Genre.描述来自此 View ,因为我认为您不想编辑它)。在此 View 的 get 方法中,将 ViewModel 与给定专辑的适当值混合,然后将其发送到 View 。在您的 post 方法中,您将根据 Album.AlbumId 从数据库中获取 Album 模型,并获取 GenreArtist,然后在保存Album 之前设置Album 模型的 ID。

其他建议

我可能会将其更改为下拉列表而不是文本框,因为现在它需要完全匹配才能获取艺术家 ID。或者,您可以将其保留为文本框,并让 Controller 检查是否存在具有该名称的艺术家,如果不存在,则在分配 ID 之前在数据库中创建具有该名称的新艺术家。

如果您有疑问,请告诉我。

关于c# - 我想为网站开发 CRUD 界面,但在 Edit 方法中不断收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333602/

相关文章:

c# - 与 Entity Framework 的松散耦合

javascript - CSS 过渡 : Cover Image From Bottom To Top

CSS div nowrap 在 IE 中不起作用

html - 如何在 Bootstrap 3 中为响应式表格添加边框

c# - 无法将嵌套关系或元素列添加到包含 SimpleContent 列的表

c# - 绑定(bind)下拉列表而不引起回发

c# - 如何在对象的 IList 中找到重复的属性值并合并它们?

c# - 使用 LINQ 从字符串数组中删除 "NULL rows"

c# - 如何将十六进制转换为字符串?

ASP.net MVC 创建具有多个选项的值(value)模型