我想在ASP.NET MVC 5中創建一個基本的論壇,我真的被困在「與FOREIGN KEY約束衝突的INSERT語句」錯誤。 我會發布我的課程和控制器這個任務和一個圖像,說明我想如何工作。試圖創建一個論壇MVC 5/ASP.NET
Thread.cs
namespace BlogProject.Models
{
public class Thread
{
[Key]
public int ThreadId { get; set; }
public DateTime? PostDate { get; set; }
public string ThreadText { get; set; }
public string ThreadTitle { get; set; }
public virtual ApplicationUser UserProfile { get; set; }
public string GetUserName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string PostTitle { get; set; }
public string PostText { get; set; }
public DateTime? PostDate { get; set; }
public virtual ApplicationUser UserProfile { get; set; }
public virtual Thread Thread { get; set; }
//[ForeignKey("Thread")]
public int ThreadId { get; set; }
}
}
PostsController.cs
// POST: Posts/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PostId,PostTitle,PostText,PostDate,ThreadId")] Post post,Thread thread)
{
if (ModelState.IsValid)
{
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Threads");
}
ViewBag.ThreadId = new SelectList(db.Threads, "ThreadId", "ThreadText", post.ThreadId);
return View("Details");
}
這是我想要的工作:
Forum goal 如果你覺得這裏的信息是不夠的,然後告訴我和我會增加更多。
在此先感謝!
UPDATE編輯:
對不起,忘了,包括我的看法
Details.cshtml,這是我的線程去
@model BlogProject.Models.Thread
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div class="container">
<h3>Current thread: @Html.DisplayFor(model => model.ThreadTitle), Posted by: @Html.DisplayFor(modelItem => Model.GetUserName)</h3>
<div class="panel panel-default">
<div class="panel-body">
<div class="col-lg-2">
<div class="thumbnail">
<img class="img-responsive user-photo" src="https://ssl.gstatic.com/accounts/ui/avatar_2x.png">
</div>
</div>
<div class="well-lg">
<div class="col-lg-10">
@Html.DisplayFor(model => model.ThreadText)
</div>
</div>
</div>
</div>
<p class="text-center">
@Html.ActionLink("Reply to this thread","Create","Posts")
@Html.ActionLink("Back to List", "Index")
</p>
</div>
帖子/ Create.cshtml(局部視圖)
@model BlogProject.Models.Post
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PostText, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostText, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostText, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
如果需要,我會發布更多視圖。對不起,花了這麼長時間回答,我大多躺在今天睡覺的沙發上。
錯誤消息告訴你,你想救不滿足於數據庫中的外鍵約束的數據。你在那張桌子上有哪些外鍵?你插入了什麼值?外鍵的值是否在相關表中有相應的值?根據錯誤,他們沒有。 – David
顯示您的查看頁面,其重要的 – anand