尝试在 MVC 5 / ASP.NET 中创建论坛

问题描述 投票:0回答:1

我正在尝试在 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");
}

这就是我想要的工作方式:

论坛目标 如果您认为这里的信息还不够,请告诉我,我会添加更多信息。

提前致谢!

更新编辑:

抱歉,忘记包含我的观点

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
1个回答
0
投票

首先更改

Post
模型,因为您没有分配
Primary Key
foreign 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
对象将为空并尝试“线程”

db.Posts.Add(post);

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.