I have an ASP.NET MVC online store with two views:
- Product page (photo, description, etc.)
- The form in which the user can leave a review
After the user successfully submits the form, he should be redirected back to the position page, and a one-time message should be displayed at the top: "Your review has been sent successfully."
The controller code (simplified) is as follows:
[HttpGet] public ActionResult ViewItem([Bind] long id) { var item = _context.Items.First(x => x.Id == id); return View(item); } [HttpGet] public ActionResult AddReview() { return View(); } [HttpPost] public ActionResult AddReview([Bind] long id, [Bind] string text) { _context.Reviews.Add(new Review { Id = id, Text = text }); _context.SaveChanges(); return RedirectToAction("ViewItem"); }
There are several requirements to satisfy:
- The message should not be displayed again if the user refreshes the item page.
- The message should not pollute the URL.
- Controller methods cannot be combined into one.
I was thinking about saving a message in a user session and discarding it after display, but could there be a better solution?
source share