views:

42

answers:

3

Again an easy question for ASP.NET MVC 2.

public ActionResult MyAction(MyModel model)
    {
        if (ModelState.IsValid)
        {
           // do great stuff and redirect somewhere else

        }

        // model has errors
        return View("~/Home/Index", model);
    }

The question is that I want to return a view which is outside of the current controller. I do not want to redirect since I want to hand the model to the next view. The "View"-method does not allow to specify a controller. The above "return View..." obviously doesn't work.

I am sure that there's a simple workaround here :)

+2  A: 

If your view is used by more than one controller put it in the Shared views folder instead of a specific controller's view folder. Then you can simply refer to it by it's name, if different than the name of the action.

tvanfosson
A: 

You'll need an extension

return View("~/Path/To/View.aspx", model);
Mika Kolari
That doesn't work.
Sparhawk
Sure it works...
Mika Kolari
+1  A: 
public ActionResult MyAction(MyModel model)
{
    if(ModelState.IsValid)
    {
        return RedirectAction("...");
    }
    ControllerContext.RouteData.Values["controller"] = "SomeOtherController";
    return View("Index");
}

will guide you do Index view under SomeOtherController folder.

Terje