ASP.NET MVC3 部分视图命名约定

发布于 2024-11-02 22:52:14 字数 512 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

德意的啸 2024-11-09 22:52:14

没有必要使用下划线,但对于不直接提供服务的文件来说,这是一种常见约定。

为了解决这个问题,您可以选择返回一个 View 或 PartialView,并以视图名称作为参数。

return View("_List");

return PartialView("_List");

或 在另一个视图中

@{ Html.RenderPartial("_List"); }

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"); }
深居我梦 2024-11-09 22:52:14

如果分部视图依赖于 ActionMethod 并且始终通过 Action 方法渲染,则您应该使用与操作方法相同的分部视图名称,如下所示

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

,但如果您的分部视图不依赖于操作方法并直接通过视图调用,如下所示

@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"); 
洒一地阳光 2024-11-09 22:52:14

首先,对于任何平台来说,新手都没有什么可耻的。这是八年前的事了,所以你可能不再是新手了。您可以使用任何您想要使用的命名约定。我采用原始的 MVC 命名约定,仅对共享视图使用下划线 (_)。部分视图应根据其操作来命名。在您的情况下,视图的名称将为 Action.cshtml ,当然,除非这是共享视图。

我的理由很简单。如果您从操作调用 View 或 PartialView 并且不提供 viewName,则它假定视图的名称是操作的名称。 _Layout.cshtml 也用下划线命名。这是因为它是共享的,而不是因为它是部分视图。这个错误在 MVC 世界中比比皆是。人们对此的看法确实是错误的。不知道为什么。命名惯例由商店自行决定。

辅助方法 Html.RenderActionHtml.Action 调用控制器上的操作。帮助器方法 Html.RenderPartialHtml.Partial 允许您将模型直接传递到 Razor 视图,而无需通过操作。

最后一件事,调用 Action 而不是 RenderAction。仅当您已位于代码块内时才会调用 RenderAction。事实几乎从来没有是这样的。我看到人们使用 RenderAction,然后在其周围添加不​​必要的代码块,只是因为构建中断了。以下两个代码片段完全相同,在我看来,第二个代码片段更具可读性。我放置 div 是为了强调代码不在代码块中:


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

<div>
    @Html.Action("List", "Student")
</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 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. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文