views:

50

answers:

1

I'm developing in asp.net mvc2. I am beginning to create a lot of views and partial views, for most of which I've had to create a view model. This is looking like soon it's going to become unmanageable to remember what view goes with what model. I've tried to use inheritance in my view models as much as possible.

I'm wondering how others manage this in their projects?

A: 

I place my PV's inside a PV folder within the Views folder.

so Views/Home/PartialViews;

I then register that path in my global.asax file;

    public static void RegisterViewEngine()
    {
        ViewEngines.Engines.Clear();

        WebFormViewEngine viewEngine = new WebFormViewEngine();

        viewEngine.PartialViewLocationFormats = (new[] {
            "~/Views/Shared/PartialViews/{0}.ascx",
            "~/Views/{1}/PartialViews/{0}.ascx"
          }).Concat(viewEngine.PartialViewLocationFormats).ToArray();

        ViewEngines.Engines.Add(viewEngine);
    }

I'm also these days leaning towards putting the FormViewModels within the views folder.

/Views/Home/IndexFormViewModel.cs

The above is recent as before this I put them in a Models project but found that sometimes I could end up with a cyclic reference situation with my Model and DataRepository.

griegs
What is the benefit of manually registering partial views?
Am
You're not. You're registering the location to the PartialViews. In the code above {1} refers to the View like /Home or /ContactMe etc. Then you can place your PV's in the PartialViews folder and not have to code in the path within your views. This is good news because you can drag a PV from say the /home/PartialViews folder to the Shared/PartialViews folder and not have to change any code.
griegs
I should add that you dont code in "~/Views/Home/PartialViews/MyPV.ascx". You literally code "~/Views/{1}/PartialViews/{0}.ascx" as is.
griegs
Thanks for clarifying that. Sounds very useful.
Am