2016-02-08 37 views
0

我想在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> 

如果需要,我会发布更多视图。对不起,花了这么长时间回答,我大多躺在今天睡觉的沙发上。

+0

错误消息告诉你,你想救不满足于数据库中的外键约束的数据。你在那张桌子上有哪些外键?你插入了什么值?外键的值是否在相关表中有相应的值?根据错误,他们没有。 – David

+0

显示您的查看页面,其重要的 – anand

回答

0

首先改变Post模型,因为你不分配Primary Keyforeign Key

public class Post 
{ 
    [Key] 
    public int PostId { get; set; } 
    . 
    public int ThreadId { get; set; } 
    [ForeignKey("ThreadId")] 
    public virtual Thread Thread { get; set; } 
} 

在控制器

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Thread thread) 

因为在视图页面您仅使用

@model BlogProject.Models.Thread 

post ob JECT将是无效和尝试“线”

db.Posts.Add(post); 

希望这有助于

+0

PostId下的点代表什么?我应该删除它和ThreadId之间的所有代码吗? –

+0

你傻。我很快就写了答案。更正在三条线上。你已经放置了所有的行。 – anand

+0

告诉我错误 – anand