如何在 ASP.NET MVC3 中组织部分视图?
我正在尝试弄清楚如何在我的项目中组织我的部分视图。有些人说在分部视图的名称前加上 _
,但如果可以直接调用视图,这会导致控制器中的操作名称奇怪。
另外,如果视图在某些情况下可以是部分视图,而在其他情况下可以是常规视图,该怎么办?
一个常见的示例是我嵌入到某些页面上以在应用程序中搜索用户的搜索视图,但我也有一个加载相同视图的搜索页面。我想我可以为搜索页面创建第二个视图,仅嵌入部分视图。只是想知道其他人在做什么。
I am trying to figure out how to organize my partial views in my project. Some people say to precede the name of a partial view with an _
, but that makes for weirdly named actions in the controller if the view can be called directly.
Also, what should be done if the view can be a partial view in some cases and a regular view in other cases?
A common example for this is a search view that I embed on some pages to search for users in my app, but I also have a search page that loads the same view. I suppose I could create a second view for the search page that just embeds the partial view. Just wondering what other people are doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
老实说,这是一个偏好问题。您应该在应用程序中尽一切努力避免代码(或视图)重复等。
我们(我是开发 MVC 团队的开发人员)建议在部分视图文件名前添加下划线的原因是为了更容易区分在 VS 中查看文件时在完整视图和部分视图之间
Honestly it's a matter of preference. You should do whatever works in your application with respect to avoiding code (or view) duplication etc.
The reason why we (I'm a dev on the team developing MVC) recommend preceding the partial view filename with an underscore is to more easily distinguish between full and partial views when looking at files in VS
我还使用带有下划线字符作为前缀的部分视图,以便在管理文件时轻松区分视图和部分视图。随着您的项目变得越来越大,您可能会有很多用于单个控制器的文件,因此这个约定将对您有很大帮助。
此外,当您使用分部视图时,您可以使用以下操作通过操作调用视图:
您必须记住,如果您将分部视图用作视图,则应根据分部工作的模式为其分配布局(作为视图或部分视图),例如模型类上的布尔属性。
I also use my partials with the underscore character as a prefix to easily distinguish between a view and a partial view when managing the files. As your project becomes bigger you may have a lot of files for a single controller, so this convention will help you a lot.
Besides, when you use a partial view you can call your views with an action using the following:
You have to remember that if you are using your partial as a View, you should assign the layout to it depending on the mode the partial is working (as a view or partial view), for example with a boolean property on your model class.