bernie2436
bernie2436

Reputation: 23901

how can I tell what view this controller will return?

I am new to MVC 4 and learning an MVC 4 codebase. In the music store tutorial from Microsoft, I added views by right clicking a method in a controller and choosing Add View.

This allowed me to know what view was linked with what controller.

But in the codebase, there is a file called homecontroller.cs w/ a method called "index" that returns a view. How can I tell what view the method returns?

    [Authorize]
    public ActionResult Index(bool preserveShowFor = false)
    {
        if (User.IsInRole("..."))
        {
            return View(new HomeViewModel...); //how do I know what view this returns?
        }

Upvotes: 1

Views: 77

Answers (5)

dave clements
dave clements

Reputation: 1505

By default, MVC will look for a view with the same name as the action result, in this case, Index

You could specify it manually by returning this instead

new View("MyViewName", new HomeViewModel())

Upvotes: 4

Ryan Drost
Ryan Drost

Reputation: 1184

The convention is that it will check the folder under Views that has the same name as your controller, with the name of your action.

So in this case, Views/Home/Index.aspx

Upvotes: 0

user1071979
user1071979

Reputation: 1791

Right click inside the method and click 'Go To View'. It should be in a folder under views called Home.

You can also pass explicit views

Upvotes: 0

Martin Milan
Martin Milan

Reputation: 6390

In this case it will look for a view called Index to match the method name... ASP.Net MVC supports a lot of conventions such as this, but you can code things explicitly if you really want to...

Upvotes: 0

Lionel D
Lionel D

Reputation: 317

The view your code will return is the Index one. The view is in a subfolder called Home.

If no view is specified then the view with the same name as the action is returned.

Upvotes: 2

Related Questions