45

I'm new to the MVC development so please bear with me. Is it really necessary to name my partial view like _Action.cshtml (with the _ underscore) to comply with the naming convention?

Here's my problem I have a controller (StudentController) and an action (List) that has a partial view file named "List.cshtml", and have

@{ Html.RenderAction("List", "Student"); } 

to display this inside my HomeController - Index view as partial view which works. But if I name my partial view to _List.cshtml of course it will not work. Visual Studio could not even find the view for my action Student - List because it think it's still looking for the exact same name as my action (List.cshtml). What should I do?

I m so used to ASP.NET ascx with a pairing ascx.cs code. :(

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
secretAgentB
  • 1,279
  • 4
  • 19
  • 36

3 Answers3

63

It's not necessary to use an underscore, but it's a common convention for files which aren't meant to be served directly.

To solve this, you do have the option of returning a View or PartialView with the name of the view as a parameter.

return View("_List");

or

return PartialView("_List");

or inside another view

@{ Html.RenderPartial("_List"); }
ataddeini
  • 4,931
  • 26
  • 34
  • @ataddeini Thanks for your reply but what I this partial view requires to run a piece of code before it can be displayed? Like this partial view has to query all the subjects/grades of the current user. Is it a good practice to call an action from the controller? – secretAgentB Apr 22 '11 at 00:16
  • 1
    So like you have in your initial post, using `@{ Html.RenderAction("List", "Student"); }` in the view? That's reasonable, as long as in your action you return `PartialView("_List", model)` you should be able to work with it like any other action. If you don't like using strings to indicate which view to return (like I do) you can consider using T4MVC which is part of the [MvcContrib](http://mvccontrib.codeplex.com/) project. Hope that helps! – ataddeini Apr 22 '11 at 00:38
  • 3
    Having successfully launched a CRM system built using MVC2 and T4MVC I would heartily recommend NOT using T4MVC. T4MVC offered me no advantage at all other than replacing strings with another type of string. The Action methods it generates also were not usable that in the end all I used were the fancy string names. Compound this with T4MVC lead to me causing our site to face downtime. I deployed the site without running the custom tool to regenerate the T4MVC files after I did a pull and build which resulted in strange runtime errors from the files being out of sync. – Chris Marisic May 17 '11 at 14:16
  • 1
    Also more advice coming from an in practice architect, the mvc conventions are pretty good I would avoid changing them without a very substantially compelling reason. MVC & Razor are a substantial shift from webforms, accept that and remember to not seek solutions from how you approached webforms with MVC. – Chris Marisic May 17 '11 at 14:32
  • @Chris: I agree that many of the properties T4MVC generates are really just magic strings. You still need to decouple them in some way from their usage, just as you would with strings. Really, the main benefit is the Intellisense discovery. Unfortunately, it's easy to fall into the "No more magic strings!" trap when you start using it and screw yourself over. – adamjford Jun 30 '11 at 17:25
  • @Chris: What did you mean by the Action methods not being usable? Are you referring to an action that accepts an int in the URL but uses a ModelBinder to translate that to a domain object or whatnot? What I do to get around that is this: say I have an action on the UsersController called View(User user). I would have (or created) a helper class that attaches itself to HtmlHelper via extension method, and make a method in that class that calls `html.ActionLink(linkName, MVC.Users.View().AddRouteValue("user", userID))`, and then call that method like `@Html.Users().ViewLink(userName, userID)`. – adamjford Jun 30 '11 at 17:30
  • @adamjford yes that is precisely the type of thing I'm talking about, needing to come up convention or code to facilitate usage like that. In retrospect I suppose you could truly alter the T4 files and have the T4MVC tool generate them all for you however I'm not very proffeccient in T4 and any of this work rapidly goes down the hole of why did I even do this? Working with MVC3 and razor and purely using strings has been no real headaches. However I do believe there is tooling support from Resharper or similar to get the intellisense completion now with raw strings and controller actions etc – Chris Marisic Jul 05 '11 at 14:33
  • Another question. So now that my partial views are with "_" prefix, should I name my PartialView functions with _ in front. Otherwise I'll have to add the view name to the return PartialView() call.. – secretAgentB Mar 11 '14 at 15:02
  • Partial views should not have an underscore (_) unless they are shared. The underscore naming convention signifies that a view is shared. Everyone gets this wrong. I have proof, _Layout is not a partial view but gets the underscore. It gets it because it is shared. Also, why does the helper method PartialView assume that the view is named directly after the action if you don't provide an view name? You can use whatever naming convention you want, but this is a myth and I'm not sure where it got started. It doesn't even make sense. – Jordan Jul 22 '19 at 15:31
3

If Partial view depends on ActionMethod and always render by Action method, you should same partial view name same as action method like this

public PartialViewResult List()
     {
        DoSomthing();
        //PartialView() return a "List "Parial View  
        return PartialView();
     }

but if your partial view not depends on the action method and directly call by view like this

@Html.RenderPartial("_List"); 
Kashif Faraz
  • 321
  • 6
  • 19
  • This makes sense to me, but are there any "official" guidelines/recommendations that support exactly this? – JohannSig May 30 '18 at 08:06
  • 1
    Yes, everyone gets this wrong. The underscore naming was originally created for SHARED views. Partial views should be named after their action. – Jordan Jul 22 '19 at 15:27
2

First, there is no shame to be new to any platform. And this was eight years ago so you are probably not new any more. You can use whatever naming conventions you want to use. I go with the original MVC naming convention which uses underscores (_) only for shared views. Partial views should be named after their actions. In your case the name of the view would be Action.cshtml unless this is a shared view of course.

My reasoning is simple. If you call View or PartialView from an action and don't provide a viewName, it assumes the name of the view is the name of the action. Also _Layout.cshtml is named with an underscore. This is because it is shared, not because it is a partial view. This mistake is all over the place in the MVC world. People are really zealously wrong about it. Don't know why. Naming convention is the shop's discretion.

The helper methods Html.RenderAction and Html.Action call actions on the controller. The helper methods Html.RenderPartial and Html.Partial allow you to pass a model directly to a Razor view without passing through an action.

One final thing, call Action instead of RenderAction. RenderAction is only called if you are already inside of a code block. This is almost never the case. I see people using RenderAction and then adding a code block around it unnecessarily just because the build breaks. These two following code snippets are exactly the same and the second one is way more readable in my opinion. I put the div to emphasize that the code is not in a code block:


<div>
    @{ Html.RenderAction("List", "Student"); } 
</div>

<div>
    @Html.Action("List", "Student")
</div>

The bottom line is don't use underscores or curly braces unnecessarily. They are ugly characters and we should avoid them. ;)

Jordan
  • 9,642
  • 10
  • 71
  • 141