ASP.NET MVC3 部分视图命名约定
我是 MVC 开发的新手,所以请耐心等待。是否真的有必要将我的部分视图命名为 _Action.cshtml
(带有 _
下划线)以符合命名约定?
这是我的问题,我有一个控制器(StudentController)和一个操作(List),它有一个名为“List.cshtml”的部分视图文件,并且必须
@{ Html.RenderAction("List", "Student"); }
在我的 HomeController - Index 视图中将其显示为有效的部分视图。但是如果我将我的部分视图命名为 _List.cshtml
当然它不会工作。 Visual Studio 甚至找不到我的操作 Student - List 的视图,因为它认为它仍在寻找与我的操作 (List.cshtml)
完全相同的名称。我应该怎么办?
我已经习惯了 ASP.NET ascx 与配对的 ascx.cs 代码。 :(
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. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有必要使用下划线,但对于不直接提供服务的文件来说,这是一种常见约定。
为了解决这个问题,您可以选择返回一个 View 或 PartialView,并以视图名称作为参数。
或
或 在另一个视图中
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.
or
or inside another view
如果分部视图依赖于 ActionMethod 并且始终通过 Action 方法渲染,则您应该使用与操作方法相同的分部视图名称,如下所示
,但如果您的分部视图不依赖于操作方法并直接通过视图调用,如下所示
If Partial view depends on ActionMethod and always render by Action method, you should same partial view name same as action method like this
but if your partial view not depends on the action method and directly call by view like this
首先,对于任何平台来说,新手都没有什么可耻的。这是八年前的事了,所以你可能不再是新手了。您可以使用任何您想要使用的命名约定。我采用原始的 MVC 命名约定,仅对共享视图使用下划线 (_)。部分视图应根据其操作来命名。在您的情况下,视图的名称将为 Action.cshtml ,当然,除非这是共享视图。
我的理由很简单。如果您从操作调用 View 或 PartialView 并且不提供 viewName,则它假定视图的名称是操作的名称。 _Layout.cshtml 也用下划线命名。这是因为它是共享的,而不是因为它是部分视图。这个错误在 MVC 世界中比比皆是。人们对此的看法确实是错误的。不知道为什么。命名惯例由商店自行决定。
辅助方法
Html.RenderAction
和Html.Action
调用控制器上的操作。帮助器方法Html.RenderPartial
和Html.Partial
允许您将模型直接传递到 Razor 视图,而无需通过操作。最后一件事,调用
Action
而不是RenderAction
。仅当您已位于代码块内时才会调用 RenderAction。事实几乎从来没有是这样的。我看到人们使用 RenderAction,然后在其周围添加不必要的代码块,只是因为构建中断了。以下两个代码片段完全相同,在我看来,第二个代码片段更具可读性。我放置div
是为了强调代码不在代码块中:底线是不要使用不必要的下划线或大括号。他们是丑陋的人物,我们应该避免他们。 ;)
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
andHtml.Action
call actions on the controller. The helper methodsHtml.RenderPartial
andHtml.Partial
allow you to pass a model directly to a Razor view without passing through an action.One final thing, call
Action
instead ofRenderAction
.RenderAction
is only called if you are already inside of a code block. This is almost never the case. I see people usingRenderAction
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 thediv
to emphasize that the code is not in a code block:The bottom line is don't use underscores or curly braces unnecessarily. They are ugly characters and we should avoid them. ;)