How to redirect a user to previous URL after submitting a form?
I am working on a questionnarie. As administrator, you are able to edit questions as you are viewing them.
A page with the questions can have following url:
http://localhost/Survey/DisplayQuestions/2/1
When the administrator pushes the "Edit" link, an Edit view is displayed. When the administrator pushes "Save", i would like to redirect him to the previous site, which in this example would be:
http://localhost/Survey/DisplayQuestions/2/1
How can i accomplish this?
The Edit action looks like this:
[HttpPost]
public ActionResult Edit(Question question)
{
if (ModelState.IsValid)
{
db.Question.Attach(question);
db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.category_id = new SelectList(db.Category, "category_id", "category_name", question.category_id);
ViewBag.type_code = new SelectList(db.Que开发者_JAVA百科stion_Type, "type_code", "type_description", question.type_code);
return View(question);
}
Thanks!
You have a controller "Survey" and action "DisplayQuestions". What you basically need is to return Admin back to this action. But, as far as I see DisplayQuestions recieves some paramerers (2/1).. Since, you have to put the same parameters to restore the same state.
I believe that Question object, contains all required info for that.. If not, it have to be extended.
I don't know how your model looks like, so I assume that Question contains Id, QuestionId field (just for intance)
[HttpPost]
public ActionResult Edit(Question question)
{
if (ModelState.IsValid)
{
// do something..
return RedirectToAction("DisplayQuestions", "Survey", new { id = question.Id, questionId = question.questionId});
}
// do something else..
return View(question);
}
That would correctly redirect to DisplayQuestions.
精彩评论