Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Partial VIews and User Controls in MVC

Can anyone please tell what's the exact differences between partial views and user controls in an MVC app? Which one is feasible to use? I am using user controls for filling my views which have one or more tabs(which i have added using Ajax control toolkit). I want to know about advantages/disadvantages while using partial views and user controls.

Thanks, Kaps

like image 419
Jaqen H'ghar Avatar asked Jan 16 '10 07:01

Jaqen H'ghar


People also ask

What is difference between view and partial view in MVC?

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

What is a partial view in 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.

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.

What is advantage of partial view in MVC?

Advantages of Partial View in ASP.net MVC:Enhances reusability by packaging up common website code instead of repeating the same in different pages. Easy to maintain. Changes in future are simple to accommodate.


2 Answers

I mostly agree with Ryan. However one point to consider though is that user controls have an implementation of events whereas partial views do not.

Kindness,

Dan

like image 122
Daniel Elliott Avatar answered Oct 22 '22 22:10

Daniel Elliott


Partial Views and User Controls are basically the same thing. User Controls are just a way of distinguishing between regular Views and Partials. When you see the "Partial.ascx", it's immediately obvious that it's a Partial because the icon is different in Visual Studio.

There's nothing stopping you from using a regular *.aspx file as a Partial. In fact, some people do exactly this, and prefix their aspx Partial names with an underscore (ex: _UserStatus.aspx).

My personal preference is to use the ascx files instead because it's easier to tell that something is a partial at a glance.

like image 33
Ryan Rivest Avatar answered Oct 22 '22 23:10

Ryan Rivest