Francis Rodgers
Francis Rodgers

Reputation: 4675

MVC3 My program is looking in the wrong location for the view

I created a project and in it worked hard and created many models, views and controllers. All worked great. I decided to rename one of my models, and accordingly rename my controller and view thinking it would all continue to work great. This is because I wanted to use the origional name for something more appriopriate later. For example.

Old Names:
MyOldModel
MyOldController
MyOldView

Were renamed to:
MyNewModel
MyNewController
MyNewView

Now all still works great execpt that when I click the link to my new view my program looks for and tries to show MyOldView which obviously does not exist. However when I manually put in /MyNewView it works.

How do I change my controller to look for the Index in the MyNewView folder instead of looking for the Index in the MyOldView folder.

I even tried deleting and recreating the controller to no avail.

Thanks in advance for any help.

Edit: To those who have been kind enough to reply so quickly something to note: The exact steps taken were:
1. Rename the model file from MyOldModel.cs to MyNewModel.cs
2. Rename the controller file from MyOldController.cs to MyNewController.cs
3. Renamed the FOLDER on the view (which only contains Index.cshtml)
from MyOldView to MyNewView
4. At each step visual studio prompted me to rename all refrences to the object being renamed and I accepted (said yes). So the class names all got updated correctly. From what I can see at least, so did all the other refrences.

According to what is being said here, it should be working.

I simply renamed the

Upvotes: 0

Views: 647

Answers (4)

RhysW
RhysW

Reputation: 453

As requested by the op, my comment as an answer:

check that your _Layout points to the right views also from button clicks otherwise it will still be searching for the old controllers/views when you have replaced/renamed them with the new controllers/views, glad to be of assistance!

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Have you done the required modifications to your routes list (registered in Global.asax file i Application_Start() event):

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Let's suppose that you have an Index action on the MyNewController:

public class MyNewController: Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Now make sure that you have placed the view in ~/Views/MyNew/Index.cshtml. That's the established convention. Notice that if your controller class is called MyNewController, the folder must be called MyNew. Also don't forget to recompile your web application after renaming the controller class.

Upvotes: 0

Richard
Richard

Reputation: 30618

Did you ensure that you renamed the class inside the Controller file, not just the file itself? i.e. so it contains

public class MyNewController : Controller {
    ....
}

Upvotes: 0

Related Questions