By Jignesh Trivedi on May 14, 2015

http://www.c-sharpcorner.com/UploadFile/ff2f08/partial-view-in-mvc/

Partial view is special view which renders a portion of view content. It is just like a user control web form application. Partial can be reusable in multiple views. It helps us to reduce code duplication. In other word a partial view enables us to render a view within the parent view.
 
The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view. If we made the change in this data (ViewDataDictionary object), the parent view's data is not affected. Generally the Partial rendering method of the view is used when related data that we want to render in a partial view is part of our model.
 
Creating Partial View

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view.


 
It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.


 
HTML helper has two methods for rendering the partial view: Partial and RenderPartial.

  1. <div>
  2. @Html.Partial("PartialViewExample")
  3. </div>
  4. <div>
  5. @{
  6. Html.RenderPartial("PartialViewExample");
  7. }
  8. </div>

@Html.RenderPartial

The result of the RenderPartial method is written directly into the HTTP response, it means that this method used the same TextWriter object as used by the current view. This method returns nothing.
 
@Html.Partial

This method renders the view as an HTML-encoded string. We can store the method result in a string variable.
 
The Html.RenderPartial method writes output directly to the HTTP response stream so it is slightly faster than the Html.Partial method.
 
Returning a Partial view from the Controller's Action method:

  1. public ActionResult PartialViewExample()
  2. {
  3. return PartialView();
  4. }

Render Partial View Using jQuery

Sometimes we need to load a partial view within a model popup at runtime, in this case we can render the partial view using JQuery element's load method.

  1. <script type="text/jscript">
  2. $('#partialView').load('/shared/PartialViewExample’);
  3. </script>

View Vs Partial View

View Partial View
View contains the layout page Partial view does not contain the layout page
_viewstart page is rendered before any view is rendered Partial view does not check for a _viewstart.cshtml. We cannot place any common code for a partial view within the _viewStart.cshtml page.
View may have markup tags like html, body, head, title, meta etc. The Partial view is specially designed to render within the view and as a result it does not contain any mark up.
Partial view is more lightweight than the view. We can also pass a regular view to the RenderPartial method.
If there is no layout page specified in the view, it can be considered as a partial view. In razor, there is no distinction between views and partial views as in the ASPX view engine (aspx and ascx).
 

Introduction to Partial View的更多相关文章

  1. [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action

    概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...

  2. MVC 之 Partial View 用法

          Partial View 顾名思义就是Html代码片段,因此可以用Partial View 把部分的Html或显示逻辑包装起来,方便多次使用. Partial View 需要放在Views ...

  3. 视图(View)与部分视图(Partial View)之间数据传递

    写ASP.NET MVC程序,我们经常需要把数据从视图(View)传递至部分视图(Partial View) 或者相反. 今天Insus.NET使用 ControllerBase.TempData 进 ...

  4. 截取视图某一段另存为部分视图(Partial View)

    在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...

  5. [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)

    Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...

  6. [转][ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action

    本文转自:http://www.cnblogs.com/willick/p/3410855.html 概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在 ...

  7. MVC+EF 随笔小计——分部视图(Partial View)及Html.Partial和Html.Action差异

    Partial View指可以应用于View中以作为其中一部分的View的片段(类似于之前的user control), 可以像类一样,编写一次, 然后在其他View中被反复使用. 一般放在" ...

  8. ASP.NET MVC中Section、Partial View 和 Child Action(转载)

    概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...

  9. [转]How to add a script in a partial view in MVC4?

    本文转自:https://stackoverflow.com/questions/14114084/how-to-add-a-script-in-a-partial-view-in-mvc4 问题: ...

随机推荐

  1. IE兼容模式与非兼容模式下jq的写法

    1.  $("#LabelRepeatType").removeAttr("disabled");                $("#LabelF ...

  2. 2、Flume1.7.0入门:安装、部署、及flume的案例

    一.什么是Flume? flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用. flume的特点: flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的 ...

  3. svn配置及基本使用

    svn软件下载地址http://subversion.apache.org/packages.html在安装TortoiseSVN时安装客户端和服务端 下以svn在windows下使用为例,linux ...

  4. Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

    python3.6.3  我在处理爬虫时候使用BeautifulSoup中遇到报错 “ bs4.FeatureNotFound: Couldn't find a tree builder with t ...

  5. 安装并配置工具以使用iOS进行构建

    Visual Studio 2015   Visual Studio文档的新家是docs.microsoft.com上的Visual Studio 2017文档 . 有关Visual Studio 2 ...

  6. jenkins使用HTML Publisher Plugin插件 拉取报告样式缺失问题解决

    ---------------------------------------------------------临时解决方案----亲测ok 要解决该问题,方式也比较简单,就是修改Content S ...

  7. Composer安装使用

    Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 1.下载 2.安装成功 3.配置地址 4.安装代码库 镜像 配置json

  8. 解决阿里云下server 2008中tomcat不能外网访问

    1. 首先这台服务器是别人给到手的服务器, 所有环境跟我讲是全新的, 然后我开始配置tomcat; 开始安装sql 2008 r22. r1安装好之后, 本机内部怎么访问都行( . 和 127.0.0 ...

  9. VC++使用TCHAR

    #ifdef _UNICODE #define tcout wcout #define tcin wcin #else #define tcout cout #define tcin cin #end ...

  10. 异步fifo的读写

    这里不讨论异步fifo是如何实现的,而是在实现fifo的前提下,对fifo的读写. 现在遇到的问题是:总线的数据不能写入fifo中,但是地址能加一. 代码如下: if( !fifo_tx_full & ...