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. FastAdmin 在线命令生成时出错的分析

    FastAdmin 在线命令生成时出错的分析 出错现象 版本环境 FastAdmin 版本:1.0.0.20180806_beta 在线命令插件版本:1.0.3 分析 2018-08-13 16:12 ...

  2. (转)Android之Adapter用法总结

    1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...

  3. 使用Collections类对 集合排序

    对Set<Object>集合进行排序 根据类型的某一个属性去排序 public Set<School> sortByValue(Set<School> set){ ...

  4. lua语法基本

    lua的下载 http://luabinaries.sourceforge.net/点击所要下载的版本比如我下的是5.3.3https://sourceforge.net/projects/luabi ...

  5. 安装ElasticSearch客户端Kibana

    安装Kibana Kibana是一个为 ElasticSearch 提供的数据分析的 Web 接口.可使用它对日志进行高效的搜索.可视化.分析等各种操作. wget https://artifacts ...

  6. 基于LVS的负载均衡实现

    一 什么是负载均衡 负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务 ...

  7. mysql 存储过程动态执行sql语句

    之前经常在程序中拼接sql语句,其实我们也可以在存储过程中拼接sql 语句,动态的执行~~ 代码如下: DROP PROCEDURE IF EXISTS SearchByDoctor;CREATE P ...

  8. 【转】3D图形引擎(DX9): FX

    做图形程序已经很多年了,积累了一些经验.来这个论坛也有好几年了,遇到问题的时候,也会在这里发帖子问,也很感谢大家的回答与帮助,希望能多认识一些朋友,大家多交流.分享.做这款项目也有蛮多年了,终于上线了 ...

  9. spring中aop原理

  10. canvas二进制字符下落

      ?   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...