72

I have a view (Index.cshtml) with a submit button. When the submit button is clicked, it calls an action (Action01) within the controller (TestController.cs) so at the end of the action I want to return to the caller (Index.cshtml) view with a custom view model as a parameter. How do I do this?

Results after first attempt using View("ViewName",model):

An error is raised, as the action is within the controller Test, so returning, it is searching for \Views\Tests\Index, and my Index page is in \Views\Home\Index.

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Test/Index.aspx
~/Views/Test/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Test/Index.cshtml
~/Views/Test/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

Final solution:

I have used return View("ViewName", model), and I have changed my directories structure as it was the problem.

5 Answers 5

125

You can directly return a different view like:

return View("NameOfView", Model);

Or you can make a partial view and can return like:

return PartialView("PartialViewName", Model);
Sign up to request clarification or add additional context in comments.

3 Comments

An error is raised as action is in a controller whose view it is under \Views\Test
I have modified my directories structure. It was the problem. Now your solution works like a charm. Thanks a lot! My accepted solution is yours as you have answered first.
My intellisense in VS2013 wasn't picking this method up, even though it seemed like it should have been there, so this is great. Thanks. Even when I tried it, it didn't pick it up until I saw your answer and typed the entire thing out. Thanks.
68

To return a different view, you can specify the name of the view you want to return and model as follows:

return View("ViewName", yourModel);

if the view is in different folder under Views folder then use below absolute path:

return View("~/Views/FolderName/ViewName.aspx");

1 Comment

you will have to use absolute path will returning view from diffrent folders. see update code.
7
            public ActionResult Index()
            {
                return View();
            }


            public ActionResult Test(string Name)
            {
                return RedirectToAction("Index");
            }

Return View Directly displays your view but

Redirect ToAction Action is performed

Comments

4

You have to specify the name of the custom view and its related model in Controller Action method.

public ActionResult About()
{            
   return View("NameOfViewYouWantToReturn",Model); 
}

Comments

1

Also, you can just set the ViewName:

return View("ViewName");

Full controller example:

public ActionResult SomeAction() {
    if (condition)
    {
        return View("CustomView");
    }else{
        return View();
    }
}

This works on MVC 5.

1 Comment

When a view is created without a backing action method, there isn't a way to access it via url correct? I get a 404 when I try, which is what I want, by I want to make sure I'm not missing something and it's available publicly. I only want it available when I specifically return it as you demonstrate. Thanks!

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.