多对多关联的编辑操作

发布于 2024-12-29 11:19:18 字数 1498 浏览 3 评论 0原文

我正在为网站上的新闻制作标签。首先使用实体​​框架代码。 PostTag关联表(PostId + TagId)是自动生成的。 这是我的模型:

public class Post
{
    public int Id { get; set; }
    //...
    public virtual ICollection<Tag> Tags { get; set; } 
}

public class Tag
{
    public int Id { get; set; }
    //...
    public virtual ICollection<Post> Posts { get; set; } 
}

问题在于为我的管理面板实现帖子编辑器操作。创建和删除操作工作正常。这是我尝试过的,它正确更新所有帖子字段,但忽略标签。

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] TagId)
{
if (ModelState.IsValid)
{
    post.Tags = new List<Tag> { };
    if (TagId != null)
        foreach (int f in TagId)
            post.Tags.Add(db.Tags.Where(x => x.Id == f).First());
    db.Entry(post).State = EntityState.Modified;  // Doesnt update tags
    db.SaveChanges();
    return RedirectToAction("Index");
}
//...

解决方案

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] TagId)
{
    if (ModelState.IsValid)
    {
        Post postAttached = db.Posts.Where(x => x.Id == post.Id).First();
        post.Tags = postAttached.Tags;
        post.Tags.Clear();                
        if (TagId != null)
            foreach (int f in TagId)
                post.Tags.Add(db.Tags.Where(x => x.Id == f).First());
        db.Entry(postAttached).CurrentValues.SetValues(post);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

感谢 gdoron 指明方向。

I'm making tags for news on web site. Using Entity Framework Code First. PostTag association table (PostId + TagId) is automatically generated.
Here are my models:

public class Post
{
    public int Id { get; set; }
    //...
    public virtual ICollection<Tag> Tags { get; set; } 
}

public class Tag
{
    public int Id { get; set; }
    //...
    public virtual ICollection<Post> Posts { get; set; } 
}

The problem is in implementing Post Editor Action for my administration panel. Create and Delete actions work fine. Here is what I've tried, it updates all the Post fields properly, but ignores Tags.

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] TagId)
{
if (ModelState.IsValid)
{
    post.Tags = new List<Tag> { };
    if (TagId != null)
        foreach (int f in TagId)
            post.Tags.Add(db.Tags.Where(x => x.Id == f).First());
    db.Entry(post).State = EntityState.Modified;  // Doesnt update tags
    db.SaveChanges();
    return RedirectToAction("Index");
}
//...

Solution

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] TagId)
{
    if (ModelState.IsValid)
    {
        Post postAttached = db.Posts.Where(x => x.Id == post.Id).First();
        post.Tags = postAttached.Tags;
        post.Tags.Clear();                
        if (TagId != null)
            foreach (int f in TagId)
                post.Tags.Add(db.Tags.Where(x => x.Id == f).First());
        db.Entry(postAttached).CurrentValues.SetValues(post);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

Thankx to gdoron for pointing the direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2025-01-05 11:19:18

我的建议:

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] tagIds)
{
    if (ModelState.IsValid)
    {            
        post.Tags = db.Tags.Where(tag => tagIds.Contains(tag.Id));
        db.Entry(post).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    // some code here
}

我没有测试过,你能帮我们确认一下吗?

My sugestion:

[HttpPost, ValidateInput(false)]
public ActionResult Edit(Post post, int[] tagIds)
{
    if (ModelState.IsValid)
    {            
        post.Tags = db.Tags.Where(tag => tagIds.Contains(tag.Id));
        db.Entry(post).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    // some code here
}

I do not tested, could you confirm it for us?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文