Reputation: 11
I run into an error that says:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Comments_dbo.Posts_PostID". The conflict occurred in database "MVCProjectApp.Models.DBPostsContext", table "dbo.Posts", column 'PostID'.
The statement has been terminated.
I am lost and not sure what to do
Here is the create comment
@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")
}
This is the Actionlink where I am passing the postid to the comment create
//
// 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);
}
here is the comment model
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; }
}
}
Upvotes: 0
Views: 67
Reputation: 7591
you need to associate the comment to the post. the simplest solution is to add a hidden field for the post id to the view.
Upvotes: 0
Reputation: 151594
I am lost and not sure what to do
You should take a look at the error you're getting. It says you're trying to insert a Comment. The foreign key constraint check that's ran by the DBMS before an insert tries to make sure the Comment has a PostID that it knows. You don't set it, so it'll default to 0, which does not exist in the Posts table, hence the error.
You should set the Comment's PostID before calling SaveChanges()
, which can be done by adding a @Html.HiddenFor(m => m.PostID)
field in the view. You then set the PostID of the model in the controller.
Upvotes: 1