【发布时间】:2016-05-18 03:59:03
【问题描述】:
我正在尝试在 ASP.NET MVC 5 中创建一个基本论坛,但我真的遇到了“INSERT 语句与 FOREIGN KEY 约束冲突”错误。 我将发布此作业的班级和控制器,以及说明我希望它如何工作的图片。
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 如果您认为这里的信息不够充分,请告诉我,我会补充更多。
提前致谢!
更新编辑:
抱歉,忘记包含我的观点
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>
Posts/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>
如果需要,我会发布更多视图。抱歉这么久才回答,我今天大部分时间都是躺在沙发上睡觉。
【问题讨论】:
-
错误消息告诉您,您尝试保存的数据不满足数据库中的外键约束。您在该表中有哪些外键?您要插入什么值?外键的值在相关表中是否有对应的值?根据错误,他们没有。
-
显示您的查看页面,它很重要
标签: c# html asp.net-mvc frameworks