我尝试向数据库添加新注释时遇到错误

时间:2012-12-05 14:09:34

标签: c# asp.net-mvc

我遇到了一个错误:

  

INSERT语句与FOREIGN KEY约束“FK_dbo.Comments_dbo.Posts_PostID”冲突。冲突发生在数据库“MVCProjectApp.Models.DBPostsContext”,表“dbo.Posts”,列“PostID”。
  该声明已被终止。

我迷路了,不知道该怎么做

这是创建评论

@model MVCProjectApp.Models.Comment

@{
    ViewBag.Title = "Create Comment";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Comments</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Message)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Timestamp)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Timestamp)
            @Html.ValidationMessageFor(model => model.Timestamp)
        </div>


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

<div>
    @Html.ActionLink("Back to List", "~/FullPost/Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是我将postid传递给评论创建

的Actionlink
//
        // GET: /Comment/Create

        public ActionResult Create()
        {
            ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title");
            return View();
        }

        //
        // POST: /Comment/Create

        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title", comment.PostID);
            return View(comment);
        }

这是评论模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCProjectApp.Models
{
    public class Post
    {
        public int PostID { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
    public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public string Username { get; set; }
        public string Message { get; set; }
        public DateTime Timestamp { get; set; }
        public virtual Post Post { get; set; }
    }
}

2 个答案:

答案 0 :(得分:1)

  

我迷路了,不知道该怎么做

你应该看一下你得到的错误。它说您正在尝试插入评论。在插入尝试确保Comment具有它知道的PostID之前,DBMS运行的外键约束检查。你没有设置它,所以它默认为0,这在Posts表中是不存在的,因此是错误。

您应该在致电SaveChanges()之前设置评论的PostID,这可以通过在视图中添加@Html.HiddenFor(m => m.PostID)字段来完成。然后,在控制器中设置模型的PostID。

答案 1 :(得分:0)

您需要将评论与帖子相关联。最简单的解决方案是为视图添加post id的隐藏字段。

相关问题