【问题标题】:Why do not my entities want to tie together?为什么我的实体不想捆绑在一起?
【发布时间】:2021-06-14 10:43:23
【问题描述】:

我正在尝试将评论与票证联系起来。为此,我在 Ticket to comment 之间创建了一对多关系。 但是,我收到一条错误消息说明。

SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_Commenents_Tickets_Ticket_Id”冲突。冲突发生在数据库“NewTracker”、表“dbo.Tickets”、列“Ticket_Id”中。

我想这是因为我在发表评论时没有得到我的 ticket_id。为了尝试解决此任务,我包含了您可以在视图中看到的 ticket_Id。 (asp-route-Id="@Model.Ticket_Id)。

在我看来,我已经在我的票中,我想发表评论,正如你在我的网址中看到的那样,我有一个 ID /Ticket/Info/32 。因此,我认为可以使用 asp-route-Id="@Model.Ticket_Id 来链接到票证 ID?

以下是我的观点:

@model WebApplication20.ViewModel.CommentVM

@{
    Layout = "_Dashboard";
    var title = "About Ticket";
}

<html>

<body id="page-top">
    <div class="card mx-auto" style="width: 18rem;">
        <div class="card-header">
            <h4><strong>Ticket Status</strong></h4> Created @Model.Ticket.TicketCreated
        </div>
        <ul class="list-group list-group-flush">
            <li class="list-group-item"><strong>Name:</strong>@Model.Ticket.TicketName </li>
            <li class="list-group-item"><strong>Descripton: </strong> @Model.Ticket.TicketDescription</li>
            <li class="list-group-item"><strong>Priority:</strong> @Model.Ticket.TicketPriority</li>
            <li class="list-group-item"><strong>Type:</strong> @Model.Ticket.TicketType</li>
            <li class="list-group-item"><strong>Status:</strong> @Model.Ticket.TicketStatus</li>
        </ul>
    </div>

    <div class="card shadow mx-auto m-3" style="width: 42rem;">
        <div class="card-header">
            <h4><strong>Comments</strong></h4>
        </div>

        <div class="row">
            <div class="col-md-4 p-4">
                <form asp-controller="Ticket" asp-action="Comments" asp-route-Id="@Model.Ticket_Id" >
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="form-group">
                        <label asp-for="Comment.Message" class="control-label"></label>
                        <input asp-for="Comment.Message" class="form-control" />
                        <span asp-validation-for="Comment.Message" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <input  asp-controller="Ticket" asp-action="Comments" asp-route-Id="@Model.Ticket_Id" type="submit" value="submit" class="btn btn-primary" />
                    </div>
                </form>
            </div>
        </div>

        @*TABLE*@

        <div class="row">

            <!-- Area Chart -->
            <div class="col-xl-8 col-lg-7">
                <div class="card shadow mb-4">
                    <!-- Card Header - Dropdown -->
                    <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                        <h6 class="m-0 font-weight-bold text-primary">Current Comments</h6>

                    </div>
                    @if (Model.Comments.Count() > 0)
                    {
                        <table class="table table-bordered table-striped" style="width:100%">
                            <thead>
                                <tr>
                                    <th>
                                        Message
                                    </th>
                                    <th>
                                        Submitter
                                    </th>
                                    <th>
                                        Created
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                
                                @foreach (var comment in Model.Comments)
                                {
                                    <tr>

                                        <td width="10%">
                                            @comment.Message
                                        </td>
                                        <td width="10%">
                                        </td>
                                        <td width="10%">
                                            @comment.Created
                                        </td>



                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                    else
                    {
                        <h5 class="text-secondary m-1">There are no comments for this ticket yet..</h5>
                    }
                </div>
            </div>
        </div>

        @*END TABLE*@

    </div>

    <div class="text-center p-3">
        <a asp-controller="Ticket" asp-route-Id="@Model.Ticket.Ticket_Id" asp-action="Create" class="btn btn-success btn-lg text-white w-30">Edit</a>
        <a asp-controller="Ticket" asp-route-Id="@Model.Ticket.Ticket_Id" asp-action="Delete" class="btn btn-danger btn-lg text-white w-30">Delete</a>
    </div>
    <!-- Bootstrap core JavaScript-->
    <script src="/TemplateInfo/vendor/jquery/jquery.min.js"></script>
    <script src="/TemplateInfo/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="/TemplateInfo/vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="/TemplateInfo/js/sb-admin-2.min.js"></script>

</body>

</html>

这是我要发表评论的视图的控制器

public IActionResult Info(int id)
{
    CommentVM t = new CommentVM();
    t.Ticket = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
    t.Comments = _db.Commenents.Where(f => f.Ticket_Id == id);
    return View(t);
}

我应该能够发表评论的控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Comments(CommentVM obj)
{
    if (ModelState.IsValid)
    {    
        _db.Comments.Add(obj.Comment);
        _db.SaveChanges(); <--- Here are where the conflict occurs
        return RedirectToAction(nameof(Index));
    }

    return View();
}

这是我的视图模型:

public class CommentVM
{
    public Ticket Ticket { get; set; }
    public int Ticket_Id { get; set; }
    public IEnumerable<Comments> Comments { get; set; }
    public Comments Comment { get; set; }
    public string Message { get; set; }
}

但我仍然收到无法绑定到 ticket_id 的错误?

有人知道为什么吗?

【问题讨论】:

  • 您在添加评论之前保存了您的票吗?
  • 是的,我愿意。当我想发表评论时,我的票已经保存了
  • 我看到您正在获取信息操作中的 cmets 列表,但我只能在视图中看到一条评论消息。您可以发布 CommonVm 和整个视图吗?否则很难理解你在做什么。
  • 我现在就去做 :)

标签: c# asp.net-mvc asp.net-core .net-core entity-framework-core


【解决方案1】:

您必须在表单标签内的视图中添加隐藏字段

<input type="hidden"  asp-for="Comment.TicketId" value="@Model.Comment.TicketId" />

并在传递之前在您的信息操作中初始化新评论

public IActionResult Info(int id)
{
    CommentVM t = new CommentVM{ Comment= new Comment {TicketId=id} }};
    t.Ticket = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
    t.Comments = _db.Commenents.Where(f => f.Ticket_Id == id);
    return View(t);
}

【讨论】:

  • 感谢您的回答!好的,我明白了,我只是想知道一件事。当我将输入设置为隐藏时,文本字段就会消失。我应该添加一个文本区域或类似的区域才能写评论吗?
  • 谢谢它解决了我的问题! :) 为了解决我的文本区域问题,我只需放置一个带有提交按钮的新文本区域,如下所示:
猜你喜欢
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2017-08-19
  • 2023-04-09
  • 2012-02-19
  • 2021-07-19
  • 2020-04-20
相关资源
最近更新 更多