【问题标题】:Why do I lose all data when I use my POST Controller?为什么我在使用 POST 控制器时会丢失所有数据?
【发布时间】:2021-10-06 14:25:29
【问题描述】:

我有不同的功能。其中之一是我应该能够编写 cmets。我能够写评论,但问题是当我发表评论时,我的视图会丢失关于我要写评论的对象的所有数据。 例如,这就是我的 URL 在/Ticket/Info/36 之前和/Ticket/Info/0 之后的样子。

这是我发表评论的控制器的样子。

 [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Comments(CommentVM obj)
        {

   
            if (ModelState.IsValid)
            {                
                _db.Commenents.Add(obj.Comment);
                _db.SaveChanges();
            }

          

            return RedirectToAction(nameof(Info), new { @id = obj.Ticket.Ticket_Id });

        }

我的信息控制器,我有我的看法

public IActionResult Info(int id)
        {

            CommentVM t = new CommentVM 
            { 
                Comment = new Comments { Ticket_Id = id },


                UserTicketList = _db.UserTickets.Include(n => n.ApplicationUser).Include(n => n.Ticket)
                .Where(n => n.Ticket_Id == id).ToList(),

                UserTicket = new UserTicket()
                {
                    Ticket_Id = id
                },

                Ticket = _db.Tickets.FirstOrDefault(n => n.Ticket_Id == id)

            };

            t.Ticket = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
            if (t.Ticket == null)
            {
                t.Ticket = new Ticket();
            }
            t.Comments = _db.Commenents.Where(f => f.Ticket_Id == id);

            List<string> tempListOfAssignedUsers = t.UserTicketList.Select(n => n.UserId).ToList();
            //Not in LINQ clause
            var tempList = _db.ApplicationUser.Where(n => !tempListOfAssignedUsers.Contains(n.Id)).ToList();

            t.DevList = tempList.Select(i => new SelectListItem
            {
                Text = i.Email,
                Value = i.Id.ToString()
            });

            //


            return View(t);

        }

如您所见,我有return RedirectToAction(nameof(Info), new { @id = obj.Ticket.Ticket_Id }); 我也测试了稍微不同的变体,例如return RedirectToAction("Info", new { Id = obj.Ticket_Id }); ,但我仍然得到相同的结果?

【问题讨论】:

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


    【解决方案1】:

    您发布的return RedirectToAction("Info", new { Id = obj.Ticket_Id }); 不适用于您的情况。我想是因为你传递的id 和参数id 字母的大写不同。因为你仍然发现了意想不到的结果。

    尝试完全相同的代码,如下所示:-

    return RedirectToAction("info", new { 
                   id = obj.Ticket_Id,
               });
    
    public ActionResult Info(int id)
    {
        //clarify code
    }
    
    

    确保您的obj.Ticket_Id, 包含id,我认为它是。它解决了您的问题。

    【讨论】:

    • 感谢您的回答!不幸的是,它似乎没有用。我用你发布的代码交换了代码。但是,我得到了和以前一样的结果
    • @Daniel 请调试您的代码并确保您的obj.Ticket_Id 包含 id。您是否为您的 info 方法使用了另一个单独的控制器?
    • 我好像没有得到身份证。我进行了更多测试,现在收到此错误消息System.NullReferenceException: 'Object reference not set to an instance of an object.' 我的 cmets 和 info 都在同一个控制器中
    • @Daniel 您到底在哪里找到了这个空异常?
    • 在我使用了我的 POST 后它返回到 Info :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多