ASP.NET MVC3 How to reference views directly from controller
Asked Answered
C

1

6

In my controller I want to specify a different View than default. Like this :

public ActionResult EditSurvey(Int32 id)
    {

        Survey survey = _entities.Surveys.Single(s => s.Id == id);

        return View("Survey",survey);
    }

But instead of specifying the view as a string ("Survey") I would like to reference it directly, so if I decide to change the name of my view later on I don't have to change this string manually.

So I'm looking for something like this :

public ActionResult EditSurvey(Int32 id)
    {

        Survey survey = _entities.Surveys.Single(s => s.Id == id);

        return View(Views.Admin.Survey,survey);
    }
Clomb answered 24/2, 2011 at 13:4 Comment(2)
retagged if you don't mind, I think would be useful to people if the t4mvc tag is also part of your question.Schall
FWIW having used T4MVC in a MVC2 application, I didn't find it offering any value. That it added ceremony to my coding that didn't actually materialize any added benefit.Shulman
S
9

Good question, there is no inbuilt support as the View() method expects a string, but there is a Nifty tool called T4MVC created by David Ebbo that does just that.

The documentation on codeplex has a manual install procedure, I would recommend getting it with NuGet package manager straight from VS2010.

Its pretty simple, the whole thing is files which you can just add to your project. (T4MVC.tt and T4MVC.settings.t4), everytime you change your code, (1) Right-click T4MVC.tt and (2) Click "Run Custom Tool".

What it does is generate a class with Subclasses, Members, Properties for all your Controllers and Views. What it even does is create strong types for all your content, like images, css, js etc. (Which I think is just awesome)

Examples:
This

@Html.RenderPartial("DinnerForm");

Would be:

@Html.RenderPartial(MVC.Dinners.Views.DinnerForm);

This:

@Html.ActionLink("Delete Dinner", "Delete", "Dinners", new { id = Model.DinnerID }, null)

Would Be this instead:

@Html.ActionLink("Delete Dinner", MVC.Dinners.Delete(Model.DinnerID))

This :

<img src="/Content/nerd.jpg" />

Would be this instead:

<img src="@Links.Content.nerd_jpg" />

You do have to Right-Click on the tt file and "Run Custom Tool" as mentioned before every time you change your views, controllers, however, if you want to automate this, Check out Chirpy which does that and more.

(Note T4MVC has aspx/mvc2 examples on the docs but works fine on MVC3 as I use in production with a MVC3/Razor app)

Also see T4MVC tag on SO.

Schall answered 24/2, 2011 at 13:8 Comment(10)
Great suggestion, but T4MVC doesn't support MVC3 and Razor yet. Or does it?Smaltite
@fencliff It doesn't necessarily need to support mvc3 as such, since it just creates classes in the back, I currently use it in a production MVC3 application with Razor and it works a treat.Schall
@giddy Neat! I looked into it when MVC3 RTM dropped a while back, and for some reason got an idea it wasn't compatible. Good to know!Smaltite
@fencliff updated my answer with Razor syntax, looks so neat!! I've been using it since Mvc3 RC2, its mostly flawless! =P And really saves you a bite in the butt for the future. (Infact they should make it part of MVC) Also, David Ebbo is just genius, just check out some of the code!Schall
Good suggestion, to bad you still have to manually edit the member name. It's an improvement though, since now errors are thrown if I change the viewname.Clomb
@ReetZweet What do you mean manually edit the name? Also, note, View Razor/aspx views are not compiled in MVC, see this on how to enabled view compilation :#5014817Schall
@giddy My code now looks like this: return View(Views.Survey,survey); If I change my viewname to SurveyNewName, I have to edit the code manually to : return View(Views.SurveyNewName,survey);Clomb
@ReetZweet ;) This is precisely what you needed from ur question right! It think you should be able to hit F2 and rename it with Visual Studio. Haven't tried it!Schall
@giddy Well, I can rename the file (Survey > SurveyNewName) .. run the custom tool for T4MVC.tt .. now it throws errors on the old member names that don't exist anymore (Views.Survey), so those I have to manually edit. All in all its a decent solution to my problem.Clomb
@ReetZweet Ah, ok, to make life even easier, In visual studio keep your cursor on Survey, then press F2, type a new name, and all the names should be updated, since its code VS understands how to rename it.Schall

© 2022 - 2024 — McMap. All rights reserved.