3

Hello I have a method set up in my controller which saves some data into a group.

When a user tries to save to that group again it returns an error and goes back to the view. However since I am using query strings I would like to return the view and add a query string to the 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();
    }

So in the return View() I would like to add the PayId and Db. I initially used return Redirect(referer) which redirected the user to the page with query strings. Since it is a redirect no error message appears.

1 Answer 1

3

I have figured it out.

I changed my ViewBag to TempData and then returned the redirect.

TempData["Error"] = $"Already belongs to Group: {GroupName}";

return Redirect(referer);

The error message was then added to View.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.