ASP.NET MVC3部分视图命名约定 [英] ASP.NET MVC3 Partial View naming convention

查看:162
本文介绍了ASP.NET MVC3部分视图命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC开发的新手,所以请多多包涵.确实有必要将我的局部视图命名为_Action.cshtml(带有_下划线)以符合命名约定吗?

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?

这是我的问题,我有一个控制器(StudentController)和一个动作(List),该动作具有名为"List.cshtml"的部分视图文件,并且具有

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"); } 

将此显示在我的HomeController-Index视图中作为有效的部分视图.但是,如果我将部分视图命名为_List.cshtml,则它当然不起作用. Visual Studio甚至找不到我的动作Student-List的视图,因为它认为它仍在寻找与动作(List.cshtml)完全相同的名称.我该怎么办?

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?

我非常习惯使用带有成对的ascx.cs代码的ASP.NET ascx. :(

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.

要解决此问题,您可以选择返回以视图名称为参数的View或PartialView.

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");

return PartialView("_List");

或在另一个视图内

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

这篇关于ASP.NET MVC3部分视图命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆