Partial View 顾名思义就是Html代码片段,因此可以用Partial View 把部分的Html或显示逻辑包装起来,方便多次使用。
Partial View 需要放在Views/Shared 目录下,任何Controlller 下的Action 或 View 都可以载入。
 
如何载入Partial View?
MVC 的 HTML 辅助方法有个专门的方法载入分部View,方法名称为Partial.
  • Partial有以下四种方式调用
方法原型
使用范例
Partial(HtmlHelper,String)
Html.Partial("CustomerListControl")
Partial(HtmlHelper,string,Object)
Html.Partial("CustomerListControl",Model)
Partial(HtmlHelper,string,ViewDataDictionary)
Html.Partial("CustomerListControl",ViewData["Model"])
Partial(HtmlHelper,string,Object,ViewDataDictionary)
Html.Partial("CustomerListControl",Model,ViewData["Model"])
 

  • 使用控制器载入分部View

public ActionResult  CustomerListControl()
{

   Return PartialView();
}
  • 使用 Html.Action 载入分部View

@Html.Action("CustomerListControl")

如何实现?
1  Models
   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace Step1.Models
{
    public class Product
    {
        public string Name
        {
            get;
            set;
        }
 
        public string Banner
        {
            get;
            set;
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Step1.Models
{
    public class OrderModel
    {
        public Customer Customer
        {
            get;
            set;
        }
        public List<Product> ProductList
        {
            get;
            set;
        }
    }
}
2  DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Step1.DAL;
using Step1.Models;
namespace Step1.DAL
{
    public class DBContext
    {
        public static OrderModel GetOrderList()
        {
            OrderModel model = new OrderModel();
            model.Customer = new Customer() { CustomerID = "10000", CompanyName = "redwave" };
            model.ProductList = new List<Product>();
            for (int i = 0; i < 10; i++)
            {
                Product p = new Product();
                p.Banner = string.Format("Banner{0}", i.ToString());
                p.Name = string.Format("ProductMame{0}", i.ToString());
                model.ProductList.Add(p);
            }
            return model;
        }
    }
}
 
3  Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Step1.DAL;
namespace Step1.Controllers
{
    public class PartialViewController : Controller
    {
        //
        // GET: /PartialView/
 
        public ActionResult Index()
        {
            var model=   DBContext.GetOrderList();
           
            return View(model);
        }
 
    }
}
 
4  Partial View
 
@using Step1.Models;
@using System.Collections;
@model IEnumerable<Product>
<table border="1" >
    <tr >
        <td>
            Name
        </td>
        <td>
            Banner
        </td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @item.Name
            </td>
            <td>
                @item.Banner
            </td>
        </tr>
    }
   
</table>
 
5  View
using Step1.Models;
@model OrderModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<div>
    <div>
        @Model.Customer.CompanyName
    </div>
    <div>
        @Model.Customer.CustomerID
    </div>
    <div>
        @Html.Partial("CustomerListControl",@Model.ProductList)
    </div>
</div>

 
6 项目结构
 
 
7 运行结果
 
 

MVC 之 Partial View 用法的更多相关文章

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

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

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

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

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

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

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

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

  5. .net mvc 设置div的动态部分视图内容 dynamic partial view

    示例效果:点击按钮,在div中 显示不同的partial view的内容 $("#btnEdit").click(function () { //动态获取相应的部分视图 var u ...

  6. ASP.NET没有魔法——ASP.NET MVC Razor与View渲染

    对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到 ...

  7. ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理

    ASP.NET没有魔法——ASP.NET MVC Razor与View渲染   对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的 ...

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

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

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

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

随机推荐

  1. js实例:验证只能输入数字和一个小数点

    分享一个javascript脚本代码,用于验证只能输入数字和一个小数点,检测数字输入是否符合要求,效果不错,有用到的朋友拿去吧. 原文地址:http://www.jbxue.com/article/1 ...

  2. Retrofit源码分析(一)

    1.基本用法 创建接口 public interface GitHubService { @GET("users/{user}/repos") Observable<List ...

  3. Mac下 Octave 中plot 无法绘制

    在coursera看机器学习课程的时候用到Octave来做数据处理,但是装了之后用plot画图时就会报错: set terminal aqua enhanced title "Figure ...

  4. WebView加载HTML图片大小自适应与文章自动换行

    http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html 在很多App中都会使用到webview,尤其是在加载新闻内容 ...

  5. html5表单验证

    表单验证为终端用户检测无效的数据并标记这些错误,是一种用户体验的优化. 下面展现浏览器自带的验证功能也可在移动端中查看: HTML部分: <!DOCTYPE html> <html ...

  6. heap c++ 操作 大顶堆、小顶堆

    在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...

  7. Java中实现PHP中的urlencode与rawurlencode

    php手册中对urlencode这样说明 在java中 URLEncoder做了这样注释 也就是说java中对星号"*"是不进行编码的 也就是说URLEncoder之后还是&quo ...

  8. Field 'id' doesn't have a default value(jdbc连接错误)

    JDBC 连接错误: 编写数据库连接增添数据时,出现以下错误: error : java.sql.SQLException: Field 'id' doesn't have a default val ...

  9. Sharepoint添加顶部导航菜单

    网站设置->导航->全局导航添加链接->设置标题和url->保存

  10. Win7 64位 MinGW环境测试SDL2.0.3

    下载MinGW版的文件 http://www.libsdl.org/release/SDL2-devel-2.0.3-mingw.tar.gz 解压放到mysys下面 运行Makefile mysys ...