views:

29

answers:

2

I just create an empty MVC2 project. Add an Area and a controller and a view to it. Include T4MVC files into the project and run the custom tool.

Everything is generated except the ViewNames for the views in the Area.

My tree structure:

Areas

  • MyArea
    • Controllers
      • MyTestController.cs
    • Views
      • MyTest
        • MyTestView.aspx
      • MySecondTestView.aspx

As you can I have views directly in Views folder and also in folders named by the controller..

Did anyone experienced something like this?

A: 

ASP.NET MVC 2 works, out of the box, using naming conventions to link views to controller actions. These conventions allow it find default views for actions in a controller.

For example, MyTestController.cs will have actions. Lets say it only has one, Test.

By default, MVC framework will look for a view called Test.aspx in a folder MyArea/Views/MyTest

If it doesn't find it there, it will look for the view Test.aspx under /MyArea/Views/Shared

Then it will look in /Views/Shared.

[I might be missing one location, am sure there are 4, but can't remember the other one... Anyway, the principle stands]

If it can't find Test.aspx in any of those locations, it will complain.

You seem to be fighting these conventions. And that will lead you into all kinds of complications. So it is best to read a good book on MVC and really learn the basics of how MVC is designed to work.

awrigley
Ok, maybe it wasn't my best example. Even if I follow the conventions I still have a problem with MVC2 Areas and T4MVC that doesn't generate the view names.If I rename the view to 'Index', same name as one of the MyTestController actions, I still can't get the T4MVC to generate the view names.It work's perfectly fine when I'm not using areas.
Coz
A: 

T4MVC definitely supports accessing the views in an Area. I just tried the following on a new project:

  • Create an Area named 'Stuff'
  • Create a Foo controller in there
  • In that controller, right click on Index() and ask it to generate a view
  • Rerun the T4MVC custom tool

After that, I'm able to write either:

    public virtual ActionResult Index()
    {
        return View(Views.Index);
    }

or

    public virtual ActionResult Index()
    {
        return View(MVC.Stuff.Foo.Views.Index);
    }
David Ebbo
Ok, I think I figured out what was going on. It has nothing to do with Areas. I tried again your example and it works, but.. If I add a second action Index2 and then run the T4MVC and just after that generate a view for this new action and then run again T4MVC.. it will not create the view name. Apparently, if the only change that needs to be added in the generated code is about Views.. it will not add it until some other content needs to be generated (like actions). Can you confirm this scenario? Thanks a lot.
Coz