Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need an underscore for partial views in asp.net mvc

Tags:

Just to distinguish between a view used inside a dialog or used in a foreach loop (customer details) ?

like image 755
Elisabeth Avatar asked Apr 25 '12 18:04

Elisabeth


People also ask

What does the underscore on _layout Cshtml mean?

Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.

What is the right way to render partial view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

When should I use partial views MVC?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

How partial views are created in ASP NET MVC?

How Partial Views are Created in ASP.NET MVC Application? Right-click on the /Views/Shared folder and Select Add -> View option from the context menu and then provide the following details Check the Create a partial View check box and click on Add button as shown in the below image

What are some examples of partial views in webforms?

Other examples are comments on blogging site, shipping and billing address in the invoice in e-commerce site, etc. If you are coming from an ASP.NET WebForms background, then you can compare the Partial views in the ASP.NET MVC Application with user controls in the ASP.NET WebForms application.

Why can’t I use underscore in the name of a page?

Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly. Other .cshtml files within Web Pages generally need to be browsable.

How to create a partial view in Salesforce?

Create a New Partial View. To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If a partial view will be shared with multiple views of different controller folder then create it in the Shared folder, otherwise you can create the partial view in the same folder where it is going to be used.


2 Answers

You don't need an underscore. It's just a convention, and MVC is very keen on using conventions.

like image 127
Erik Funkenbusch Avatar answered Sep 20 '22 08:09

Erik Funkenbusch


Mike Brind has put this nicely in the question Why does Razor _layout.cshtml have a leading underscore in file name?:

Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.

Besides that, I find it very helpful to use this convention to differentiate between full views and partial ones.

like image 31
Marius Schulz Avatar answered Sep 20 '22 08:09

Marius Schulz