tags:

views:

82

answers:

4

What code does Visual Studio add (and where is it put?) when you right-click the controller method to link to the view?

How can one do this (link the controller & view) without Visual Studio?

+1  A: 

Visual Studio will create a Folder (if it doesn't already exist) under ~/Views/{YourControllerName} and put your view in there. If it doesn't find it in there it will look in the ~/Views/Shared folder. If you want to manually create a view you need to add your page to one of those folders, preferably the ~/Views/{YourControllerName} folder. Hit up the NerdDinner tutorial to see this in action.

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

DM
Yes, i found the folder, and i saw those files, but i wanna know where are the link (if are any) between the aspx file and the controler method. Mi final goal is not depend of that VS wizard
Kristian Damian
+1  A: 

Visual Studio uses templates to create the default views. The templates are located in the [Visual Studio Install Directory]\Common7\IDE\ItemTemplates[CSharp|VisualBasic]\Web\MVC\CodeTemplates folder.

If you wish to create an MVC .ASPX page manually, you need to simply create a blank page and provide a Page directive with the following attributes:

  • Language ("C#" or "VB")
  • MasterPageFile (default is ~/Views/Shared/Site.Master)
  • Inherits (for strongly-typed models, use ViewPage<ModelClassName>; otherwise ViewPage)

Example:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ListCompanyManagerDetailsViewModel>" %>

For user controls (.ASCX), the same rules apply, except the MasterPageFile attribute is not used and you inherit from ViewUserControl.

Example:

<%@ Control Language="C#" Inherits="ViewUserControl<Contact>" %>

P.S. The reason that namespaces do not precede any of my class names is because I declared them in the section of my web.config.

Neil T.
+3  A: 

It is all by convention. You place your views in the Views/ControllerName folder for every controller and that's the default location for framework to look for. But it is not a must in any way.

When in your controller you write

return View();

Framework assumes you want the view with the same name as action name and looks for it in Views/Controller/ folder. Then Views/Shared.

But in your actions you can write

return View("ViewName");

Framework will look for a View named "ViewName" then in same folders.

So default name for a view would be the name of action being executed. And that's a convention.

Alexander Taran
+1  A: 

By default asp.net MVC uses FormViewEngine, which is an implementation of IViewEngine. IViewEngine has got two methods called "FindView" and "FindPartialView" which actually locates the view file from "Views/Controller/" folder.

Thanks,
Rajeesh

Rajeesh
+1 for a very good answer
Kristian Damian