Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3 Partial View naming convention

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. :(

like image 436
RyanMAd Avatar asked Apr 21 '11 23:04

RyanMAd


People also ask

Can we use script in partial view?

A Note About Script Binding for Partial ViewsJavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view. This happens when loading the partial view with a @Html. Action helper method, as in this section from Edit.

What is difference between partial and render partial view?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

How many ways we can call partial view?

Within a markup file, there are two ways to reference a partial view: Asynchronous HTML Helper. Synchronous HTML Helper.


1 Answers

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"); } 
like image 102
ataddeini Avatar answered Sep 22 '22 21:09

ataddeini