ASP.NET MVC3 Partial View naming convention
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 w开发者_运维技巧ill 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. :(
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"); }
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");
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. ;)
精彩评论