Introduction to Partial View
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.
- <div>
- @Html.Partial("PartialViewExample")
- </div>
- <div>
- @{
- Html.RenderPartial("PartialViewExample");
- }
- </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:
- public ActionResult PartialViewExample()
- {
- return PartialView();
- }
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.
- <script type="text/jscript">
- $('#partialView').load('/shared/PartialViewExample’);
- </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的更多相关文章
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
- MVC 之 Partial View 用法
Partial View 顾名思义就是Html代码片段,因此可以用Partial View 把部分的Html或显示逻辑包装起来,方便多次使用. Partial View 需要放在Views ...
- 视图(View)与部分视图(Partial View)之间数据传递
写ASP.NET MVC程序,我们经常需要把数据从视图(View)传递至部分视图(Partial View) 或者相反. 今天Insus.NET使用 ControllerBase.TempData 进 ...
- 截取视图某一段另存为部分视图(Partial View)
在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...
- [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 ...
- [转][ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
本文转自:http://www.cnblogs.com/willick/p/3410855.html 概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在 ...
- MVC+EF 随笔小计——分部视图(Partial View)及Html.Partial和Html.Action差异
Partial View指可以应用于View中以作为其中一部分的View的片段(类似于之前的user control), 可以像类一样,编写一次, 然后在其他View中被反复使用. 一般放在" ...
- ASP.NET MVC中Section、Partial View 和 Child Action(转载)
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
- [转]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 问题: ...
随机推荐
- ASP.NET 2.0缓存
MSDN上缓存概述: http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx 一.页输出缓存 1.设置 ASP.NET 页缓存的两种 ...
- php webservice服务端和客户端的实现
1.创建类文件service.class.php,service类,添加若干方法. 2.用浏览器访问create_wsdl.php文件,生成service.wsdl文件. 3.修改wsdl文件,loc ...
- Spring读取properties资源文件
我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件.现在介绍下利用Spring内置对象来读取资源文件. 系统启动时加载资源文件链路:web.xml --&g ...
- Yii2 Post请求的时候出现400错误
Bad Request (#400) Unable to verify your data submission. http://www.yiiframework.com/forum/index. ...
- TabControl控件用法图解
1.首先创建一个MFC对话框框架,在对话框资源上从工具箱中添加上一个TabControl控件 2.根据需要修改一下属性,然后右击控件,为这个控件添加一个变量,将此控件跟一个CTabCtrl类变量绑定在 ...
- Ceph recover的速度控制
前言 磁盘损坏对于一个大集群来说,可以说是必然发生的事情,即使再小的概率,磁盘量上去,总会坏那么几块盘,这个时候就会触发内部的修复过程,修复就是让不满足副本要求的PG,恢复到满足的情况 一般是踢掉坏盘 ...
- Python re模块与正则表达式的运用
re模块 永远不要起一个py文件的名字,这个名字和你已知的模块同名 查找 findall(): 匹配所有 每一项都是列表中的一个元素 语法 : findall(正则判断条件,要判断字符 ...
- 02-26C#三级省市区ajax联动控件,利用UpdatePanel,以及页面取值
第一步:设置界面 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PCAC ...
- 前端学习笔记一:什么是W3C?
俗话说好记性不如烂笔头,最近在学习前端技术,一些理论性的知识虽然理解,但有时确不能精准的用语言表述出来,那就索性记下来吧,以备以后时常查看: 我们平时说的W3C,其实是World Wide Web C ...
- kibana-4.6.3-linux-x86_64.tar.gz的下载(图文详解)
对于Kibana ,我们知道,是Elasticsearch/Logstash/Kibana的必不可少成员. 第一步:进入Elasticsearch的官网 https://www.elastic.co/ ...