0

I have 2 Action methods in one controller,

Index:

public ActionResult Index(string url)
{
   // take the url as a param and do long tasks here  
   ViewBag.PageTitle = "title";  
   ViewBag.Images = "images";  
   // and some more view bags  
   return View();
}

This index view contains a form which post to another method in the same controller.

public ActionResult PostMessage(string msg, string imgName)  
{  
   // save data in the db
   // but on error I want to navigate back to the Index view but without losing data the  user fielded before submit the form.
  // Also need to pass an error message to this index view to show
}

How to return back to Index view if something went wrong in the PostMessage method, and also don't clear the form fields, plus showing an error message which the PostMessage method specified.

I need to know the best practice for doing such a scenario.

3 Answers 3

1

The best approach is usually to create a ViewModel type for your form. Add attributes to the properties of that model to define what would make it "wrong." Make your form use methods like @Html.TextBoxFor the various fields. Then have your PostMessage class take an object of that type, rather than taking the message and image name directly. Then you can validate the model and return the view again if the model is invalid.

See http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx for some code examples following this pattern.

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

Comments

1

You could specify the name of the view you want to return:

public ActionResult PostMessage(string msg, string imgName)  
{
    if (SomeErrorWhileSavingInDb)
    {
        // something wrong happened => we could add a modelstate error
        // explaining the reason and return the Index view.
        ModelState.AddModelError("key", "something very wrong happened when trying to process your request");
        return View("Index");
    }

    // everything went fine => we can redirect
    return RedirectToAction("Success");
}

2 Comments

and what about the ViewBag(s) I was sending from the index method to the index view to show, in this case they are null.
@Amr ElGarhy, you will have to set them once again in the PostMessage in the case you redisplay the same view. By the way you should not use any ViewBag. I would recommend you to use view models.
0

Just redirect back to the Index action

return RedirectToAction("Index");

There are overloads for this method that allows you to pass route values and other information.

1 Comment

This way he will loose all the values that have been entered by the user in the input fields. Kind of annoying if you have to start over everytime there is a server side error.

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.