22

Just to distinguish between a view used inside a dialog or used in a foreach loop (customer details) ?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321

3 Answers3

33

You don't need an underscore. It's just a convention, and MVC is very keen on using conventions.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 9
    @tsemer - wrong, see my comment to Marius, this was addressed already if you had bothered to read them. The underscore is only necessary if you're using the Web Pages framework, it's just a convention in MVC based on the Web Pages coding standard and has no useful function in MVC. Also see KennyZ's answer, which he recognizes that my answer is correct... For MVC. – Erik Funkenbusch Jul 01 '14 at 14:29
  • 5
    apologies for not reading the entire comments section. I'll remove the downvote when I can (21 days unless you edit). Regardless, since your answer confused me too, may I recommend to edit it to clarify "In MVC it's so and in WebPages it's so". Then I will also upvote it as the best answer :). Granted WebPages is irrelevant to the exact OP's question, but still I believe that would be most useful for whoever hits this page. – tsemer Jul 02 '14 at 06:43
  • @tsemer But the question is tagged for MVC, and not for WebPages. If the OP always has to explain the context, what use are the tags? – ProfK Feb 09 '17 at 10:21
  • 2
    I pre-answered that in the last sentence of my previous comment :) Sometimes it's useful to mention a neighbouring scope, especially if it may be a source of confusion for other people. You do not have to add it, but I think it may be useful for many people if you do. Unfortunately I cannot seem to change my downvote unless you edit your answer. Feel free to add a space somewhere and then I can remove the downvote. And as I said, I will gladly upvote if you choose to add the mentioned distinction. – tsemer Feb 09 '17 at 14:52
29

Mike Brind has put this nicely in the question Why does Razor _layout.cshtml have a leading underscore in file name?:

Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.

Besides that, I find it very helpful to use this convention to differentiate between full views and partial ones.

Community
  • 1
  • 1
Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
10

@Marius Schulz gives a nice reference, but then misses the point. Yes, the underscore helps to differentiate between full views and partial ones, but more importantly, it prevents partial views from being loaded directly by their URL, which could provide some potentially ugly results! (Like no css, for starters.)

EDIT: Mystere Man is right...what was I thinking? URLs in MVC point to controller/action, not to view.

Also, it is possible to mess things up and display a partial in a seperate window, so the naming convention does not prevent that. @Marius Schulz and I had the same misinterpretation of his quote.

The leading underscore is a useful convention to differentiate full and partial views, and I will continue to use it, but is is just a convention, not a functional difference.

KennyZ
  • 907
  • 5
  • 11
  • 1
    This is the correct answer, it is more than just an MVC convention. – Ed DeGagne Apr 25 '12 at 19:28
  • 2
    @KennyZ: True, which is why the blockquote says *And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.* – Marius Schulz Apr 25 '12 at 19:33
  • @Marius, sorry for the nitpicking, I guess I felt like your follow up after the block quote de-emphasized that bit, and made it sound more like the "just a convention" answer. – KennyZ Apr 25 '12 at 19:40
  • @KennyZ: I see what you're saying — I edited my answer and clarified my point. – Marius Schulz Apr 25 '12 at 21:23
  • 7
    @EdDeGagne - no, it's not the correct answer. MVC prevents *ALL* views from being loaded directly, underscore or not. The underscore was originally used in ASP.NET Web Pages, where views could be load directly, but this doesn't apply to MVC. – Erik Funkenbusch Apr 26 '12 at 01:40
  • @Mystere Man--I edited my answer when I realized you were right...I'd upvote yours if I had the points. – KennyZ Apr 26 '12 at 16:02