Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'View' does not exist in the current context

Tags:

c#

asp.net-mvc

When compiling, I get this error: The name 'View' does not exist in the current context in reference to my code return View();.

Full example code:

namespace Controllers
{
public class FilePageController
{
    //
    // GET: /FilePage/
    public ActionResult Index()
    {
        return View();
    }
}
}

I have done this a couple times and been unable to find an answer on SO, so I wanted to post this along with the answer, in case it helps others who have done the same thing as me while learning MVC.

like image 969
levininja Avatar asked Nov 15 '13 22:11

levininja


2 Answers

The controller is not inheriting from the controller class. MVC does many things by convention, but naming your class with "Controller" on the end is not enough.

Change it to public class FilePageController : Controller.

By the way, Controller class inherits from ControllerBase class. Hence, members of ControllerBase class are accessible from class inherited from Controller class.

like image 101
levininja Avatar answered Nov 04 '22 13:11

levininja


  1. Web Api doesnt contain View so when you create new class inside action method return View() use inherited Controller not ControllerBase
like image 1
Farukh Shaikh Avatar answered Nov 04 '22 15:11

Farukh Shaikh