试图在MVC 5 / ASP.NET中创建一个论坛

时间:2016-02-08 13:10:18

标签: c# html asp.net-mvc frameworks

我正在尝试在ASP.NET MVC 5中创建一个基本论坛,我真的陷入了“与FOREIGN KEY约束冲突的INSERT语句”错误。 我将发布我的课程和控制器来完成这项任务,并提供一个图像,说明我希望如何工作。

Thread.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");
}

PostsController.cs

@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>

这就是我希望它发挥作用的方式:

Forum goal 如果您认为此处的信息不充分,请告诉我,我会添加更多信息。

提前致谢!

更新编辑:

抱歉,忘了包含我的观点

Details.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>

帖子/ Create.cshtml(部分视图)

QML

如果需要,我会发布更多观看次数。很抱歉花了这么长时间才回答,我大部分时间躺在沙发上睡觉。

1 个答案:

答案 0 :(得分: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对象将为null并尝试'thread'

db.Posts.Add(post);

希望这会有所帮助