1
votes

I'm working on a web project in Visual Studio 2012, using ASP.NET MVC and C#.

I've a ResultController where you can add, delete or edit results Objects (--> result of a SportsEvent). On several other pages I've a link to the Add method of the ResultController. But after I submit a result on the Add page, I'm redirected to the index page of the controller.

Here is my code of the ResultControllers Add method:

public ActionResult Create()
        {
            ViewBag.EventId = new SelectList(db.Events, "EventId", "Name");
            ViewBag.AthleteId = new SelectList(db.Athletes, "AthleteId", "Name");
            ViewBag.MeetingId = new SelectList(db.Meetings, "MeetingId", "Name");
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "Name");
            return View();
        }

        //
        // POST: /Result/Create

        [HttpPost]
        public ActionResult Create(Result result)
        {
            if (ModelState.IsValid)
            {
                db.Results.Add(result);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

ViewBag.EventId = new SelectList(db.Events, "EventId", "Name", result.EventId);
ViewBag.AthleteId = new SelectList(db.Athletes, "AthleteId", "Name", result.AthleteId);
ViewBag.MeetingId = new SelectList(db.Meetings, "MeetingId", "Name", result.MeetingId);
ViewBag.StudentId = new SelectList(db.Students, "StudentId", "Name", result.StudentId);
            return View(result);
        }

What I want to achieve
If the add succeeds I want to be redirected to the page where I originally came from.

I.e. if I click on a "addResult" link on page "x", I want to be redirected to page "x", after the submit of the result.

How can I realize this?

Thank you :)

1

1 Answers

2
votes

Instead of:

RedirectToAction("Index");

You could use something like:

return new RedirectResult(Request.UrlReferrer.AbsoluteUri);