Why use ASP.NET MVC partial View (.ascx)

As stated in this thread. What are the reasons and scenarios, why am I adding a view for the controller method, that I have to select the Create partial view (.ascx) check box?

+6
asp.net-mvc
source share
3 answers

As @Brandon points out, you are using PartialView for reuse and readability.

Take, for example, a script in which you have an IQueryable contact list.

You will have a partial view that looped around the list and a partial view that displayed items.

When you do this, you can write code that allowed the partial view of the loop to decide which partial view the contact should display if there is more than one way of presenting the data.

If you place these partial views on a shared flash drive, they can be used throughout the application.

Alternatively, you can use the AJAX / jQuery call for action controllers. This action will then return a PartialView, which can then be displayed on the screen. Makes your site very attractive if you do not refresh the entire page.

+1
source share

The two main reasons are reuse and readability.

If you plan to have the same information on multiple pages, put it in the view, as in UserControls in WebForms.

If your page is massive, then it might also be a good idea to break sections into Views. They will be smaller and easier to read and maintain.

As for creating the view specifically "for the controller method", I personally do not personally create a partial view with the intention of using it directly as a result of the controller method. This usually happens later when you realize that you may have to move a little.

+2
source share

You can use Partial Pages (.ascx files) to:

  • Implement a sidebar with shared links across multiple pages on a website.
  • Avoid duplicate patterns (as described in the NerdDinner Example )

The purpose of using partial pages is to follow the Do Not Repeat Yourself ( DRY ) principle. Instead of viewing the output view again, you can create a partial view several times. It improves usability and readability.

+1
source share

All Articles