代码之家  ›  专栏  ›  技术社区  ›  theJ

带查询字符串的返回视图

  •  3
  • theJ  · 技术社区  · 6 年前

    您好,我在控制器中设置了一个方法,它将一些数据保存到一个组中。

    当用户再次尝试保存到该组时,它会返回一个错误并返回到视图。但是,由于我使用的是查询字符串,所以我希望返回视图并向URL添加查询字符串。

            public async Task<IActionResult> Create([Bind("Id,GroupId,PayCompId,Client")] PayComponentGrouping payComponentGrouping)
        {
            string referer = Request.Headers["Referer"].ToString();
    
            var GroupId = payComponentGrouping.GroupId;
            var PayId = payComponentGrouping.PayCompId;
            var Db = payComponentGrouping.Client;
    
            if (ModelState.IsValid)
            {
                IList<PayComponentGrouping> items = _context.PayComponentGrouping
                    .Where(o => o.GroupId == GroupId)
                    .Where(o => o.PayCompId == PayId)
                    .Where(o => o.Client == Db)
                    .ToList();
    
                var GroupName = _context.payComponentGroups
                                    .Where(o => o.GroupId == GroupId)
                                    .Select(o => o.GroupName)
                                    .FirstOrDefault();
    
                if (items.Count == 0)
                {
                    _context.Add(payComponentGrouping);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("Details", "DatabaseLists", new { id = Db });
                }
    
                ViewBag.Error = $"Already belongs to Group: {GroupName}";
    
            }
    
            return View();
        }
    

    所以在返回视图()中,我想添加payid和db。我最初使用了返回重定向(referer),它将用户重定向到带有查询字符串的页面。由于是重定向,因此不会显示错误消息。

    1 回复  |  直到 6 年前
        1
  •  3
  •   theJ    6 年前

    TempData["Error"] = $"Already belongs to Group: {GroupName}";
    
    return Redirect(referer);