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. Kreyos 资料收集

    Kreyos 资料收集 使用 默认需要连接 APP 解锁. 固件 https://github.com/kreyosopensource/KreyosFirmware Android 源码 https ...

  2. 编程实现从hadoop上下载

    下载 package hadoopTest; import java.io.File; import java.io.IOException; import org.apache.hadoop.con ...

  3. poj 3590 The shuffle Problem——DP+置换

    题目:http://poj.org/problem?id=3590 bzoj 1025 的弱化版.大概一样的 dp . 输出方案的时候小的环靠前.不用担心 dp 时用 > 还是 >= 来转 ...

  4. erlang 应用获取系统参数

    很多时候,我们的程序需要一些预定义的参数,比如上次说的tcp_server的例子 一般参数有几种途径,具体参考这里http://blog.yufeng.info/archives/2852 app里面 ...

  5. spring-session之二:简单配置

    官方示例:https://docs.spring.io/spring-session/docs/current/reference/html5/#samples 配置Spring Session 在W ...

  6. Angular 6 HMR 热加载配置

    什么是 HMR? ​ HMR 是hot module replacement 的简称,直译:热模块替换,如果不开启HMR模式,angular项目在模块更改的时候会从根节点开始刷新,开启HMR模式以后, ...

  7. 初识 Julia

    Ubuntu 下安装 Julia 环境 sch01ar@ubuntu:~$ sudo apt install julia 安装完成后打开 Julia 的交互式会话 sch01ar@ubuntu:~$ ...

  8. Deep Learning 学习笔记(6):神经网络( Neural Network )

    神经元: 在神经网络的模型中,神经元可以表示如下 神经元的左边是其输入,包括变量x1.x2.x3与常数项1, 右边是神经元的输出 神经元的输出函数被称为激活函数(activation function ...

  9. Git第三方仓库安装方式(IUS)

    1.安装使用里面说的自动化安装脚本 curl https://setup.ius.io | sh 2.然后可以看到 git2u相关内容 yum search git 3.执行安装,并查看下版本 yum ...

  10. Eclipse修改XML默认打开方式

    用Eclipse开发Android的时候 默认的XML是采用Android xml editor 打开,这个工具不够直观,如果想直接看文本的XML的话,可以通过如下方式修改 1.菜单:Window   ...