21

In VS 2015, for my Web.API project, I installed MVC 5 from nuget via npm. This installation added references and generated web.config file under Views folder.

I added new Controller with generated CRUD functions. When I right click and go to View on action it goes with no problem. But reverse action does not work. If I navigate to view via browser it works as well. Web app compiles as well.

Is there any way for me to fix this navigation problem in VS? Did I forget to add something?

Following Works

this works with no problem

Following gives Error:

this does not work

error

P.S: If I create brand new sample MVC 5 app both actions work as expected, this only happens Web.API project where MVC 5 added later on via nuget.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • I have the same issue. no resolution – djack109 Jun 20 '17 at 07:04
  • In Python web frameworks every action has reference to relevant HTML file (or view). I don't know why ASP.MVC keeps the record of this mapping hidden. – Suat Atan PhD Aug 11 '17 at 08:46
  • 2
    @SuatAtanPhD I agree. This just sucks. There should be a simple reference point between them. This just does not make sense to hide it into implementation details. – Teoman shipahi Aug 11 '17 at 14:04
  • I can't reproduce the issue using VS 2017 and MVC 5.2.3! It works as expected. – Salah Akbari Aug 25 '17 at 09:32
  • Maybe try posting some code. The controller and how you are doing your routing? – Buh Buh Aug 25 '17 at 10:21
  • Works fine in the vs 2015 update 3 – Alexander Mokin Aug 26 '17 at 19:39
  • Have you tried removing most of the code from both the view and the controller, to make it bare bones ? If it works then, try to add the code back in bits by bits until it stops working again. I tried to reproduce your issue in my projects but I only get the error when I try to go to the controller from a partial view ... – Mathieu VIALES Aug 27 '17 at 06:55
  • Can you provide a link to a GitHub repo or something like that so I can try to reproduce on my own machine ? – Mathieu VIALES Aug 27 '17 at 06:55
  • Are you using PartialView()? – VIGNESH ARUNACHALAM Aug 29 '17 at 12:26
  • As I can see from your print, `"ViewBag"` is still not recognized by the view. This happens every time you open a mvc view right after you open VS. I'm not an expert about it, but this most likely happens because VS hasn't fully loaded its Razor dependencies into memory yet. So, open your view and wait for the red underline to go away, then try the "go to controller" shortcut. Let me know what happens next. – Tiramonium Aug 29 '17 at 12:38
  • Have you tried checking the project type identifiers in the csproj file? – Surya Pratap Aug 30 '17 at 07:56

7 Answers7

4

This happened to me once. Delete the view and try adding the view again by right clicking on the controller action method and clicking Add View again. I think it would solve the problem

Sharthak Ghosh
  • 576
  • 1
  • 9
  • 22
4

It happens while the controller or view may be not exactly in the controller folder or view folder respectively. It may contain an additional folder. You can solve this issue through adding a new view and delete the old view.

Alsamil Mehboob
  • 384
  • 2
  • 6
  • 24
4

There appears to be a default shortcut key Ctrl-M then Ctrl-G.

This will automatically switch between the controller and view page. If you're on the controller/action it will switch to the view and vice versa.

3

My situation is not exactly the same as your (I added a new controller and new view), but I was receiving the same error message. Ensure that the name of your controller matches the name of the folder that the view is in.

For example, if the controller is TodoController.cs ensure that the view is in a folder called Todo. It doesn't seem to matter what the view .cshtml file is called. That's what fixed it for me.

Brendan L
  • 1,436
  • 14
  • 13
1

I had this exact scenario when my controller file name did not match the controller class name:

I had a controller class named InvoicesController, but the file name was InvoiceController.cs. After renaming the file name to the class name of the controller the switching back from the view to the controller started working

feihtthief
  • 6,403
  • 6
  • 30
  • 29
0

I just had this and the controller was in the same folder as the Controllers folder. I moved it into the folder and it worked after that. At least reading this, I found the shortcut key to switch between controller and view :-)

Bill.F
  • 41
  • 3
0

I too had the same issue, but then I realized that the name of controller and view should be same or else it wont work. Try renaming the controller method or rename the view.