What is the difference between a View and a PartialView in ASP.NET MVC?

后端 未结 6 1598
粉色の甜心
粉色の甜心 2020-12-16 09:28

What is the difference between a View and a PartialView in ASP.NET MVC?

At first glance the need for both seems non-obvious to me.

6条回答
  •  伪装坚强ぢ
    2020-12-16 09:53

    In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.

    In practice, the answer is: Very little.

    In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.

    Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the and tags in a standard View.

提交回复
热议问题