What is the point of Partial Views in Asp.net MVC

2019-02-05 19:31发布

问题:

Ive noticed that there seems to be no real difference between a view and a partial view. For instance, one can create a view but can render it as a partial view by using

@Html.Partial("ViewName")

or by specifying that its action return it as

return PartialView();

Ive noticed that the opposite is also the case - ie, one can create a partial view but if it is returned as a full view, it will be displayed with the default layout for the views.

My question is this - When adding a new view in Visual Studio, one is given the option of creating a view that is partial or not. Isn't this redundant, since a view can be rendered as both a partial and a full view anyway?

回答1:

No difference - it's true. But when you say "Partial View" all your teammates understand that you mean reusable views that will be used in many places across the website.



回答2:

There is difference between views and partial views, and the difference is more about their usage, rather than technical.

View is meant to be used as full page of your application, it needs layout, <html> and <title>. Partial views are more like reusable parts of other views. Partials do not represent full pages, they are inserted into other views.

From technical point of view, return View("SameView"); renders view including layout page, and returning that same view by return PartialView("SameView"); renders contents, but omits contents of layout page.



回答3:

Think of partial views as user controls in ASP.NET WebForms. Partial views are used if you want to have a functionality centralized, so it can be used in many parts of your website. This is the purpose of partial views.

Hope I have answered your question.



回答4:

Two things. First, to an extent you are right. But it's more of a semantic thing to seperate reusable code. It also comes in handy when for e.g. say you need to display a dialog but only when the user has some sort of an interaction with the page, like the click of a button. With partial views you don't have to have the markup for this on the page when it loads thereby reducing the file size. When you write markup/code in the partial view, you don't have to do the whole <html></html> code block. Instead you just create a <div></div> or whatever you need.

The bit about creating a view in Visual Studio. No, it's not redundant because when you create a partial view, it does not use your master layout file.



回答5:

Practically , there is no difference among them. But when you acknowledge an html object as Partial View then, it is considered as a self-contained object which may get serve at different places just like a web-part/User-Controls and also its lightweight.



回答6:

Partial view kept to use as partial page of the main page(parent page).
What does mean of partial view? Actually in the main page we will have all the HTML page attributes as below:
html lang="en"
head
title
meta
body

But in partial view we will not have all above attributes.

Find the features of partial page:
1. Partial page will be light wait and get fitted into the any view.
2. This will use as the reusable component.
3. Partial view will be render inside of a View(parent view or page).

For all who coming from ASP.Net background they can understand partial view as user control.

Thanks Afazal mdafazal@gmail.com



回答7:

To answer your question specifically, when adding a new view in Visual Studio, you will get some very basic markup generated for you as a starting point, based off of your selections in the dialog.

Here is the generated markup in Visual Studio 2010 (VB.NET) for the different combinations of the "Partial" checkbox and the "Layout" checkbox:

# "Create as a partial view" unchecked
# "Use a layout or master page:" unchecked

@Code
    Layout = Nothing
End Code

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>MyView</title>
</head>
<body>
    <div>

    </div>
</body>
</html>
# "Create as a partial view" unchecked
# "Use a layout or master page:" checked

@Code
    ViewData("Title") = "MyView"
    Layout = "~/ThePath/ToThe/Layout.vbhtml"
End Code

<h2>MyView</h2>
# "Create as a partial view" checked
# "Use a layout or master page:" greyed out

# returns an empty file

As you can see there is nothing fancy going on in the background or special properties being set in a secret file somewhere. The options are simply used to get some default markup on the page. Whether or not this is practical is purely subjective!



回答8:

Quite late but might be useful for someone with the same question. Partial views are helpful in a scenario where you want to load a view based on some user selection.

For instance, let's assume there is a dropdown in parent view displaying three operations that the user can perform. Based on the user selection, a partial view can be loaded into the parent view instead of keeping hidden DIVs in the parent view itself, thus making the parent view light. This will be very useful when we have multiple such user selections based DIVs