建设大型网站需要什么硬件,销氪crm,手机做网站过程,樊城网站建设Partial View 顾名思义就是Html代码片段#xff0c;因此可以用Partial View 把部分的Html或显示逻辑包装起来#xff0c;方便多次使用。Partial View 需要放在Views/Shared 目录下#xff0c;任何Controlller 下的Action 或 View 都可以载入。如何载入Partial View?MVC 的 … 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 载入分部ViewHtml.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 ListProduct ProductList { get; set; } }}2 DALusing 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 ListProduct(); 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 Controllerusing 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 IEnumerableProducttable border1 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 Viewusing Step1.Models;model OrderModel{ ViewBag.Title Index;}h2Index/h2div div Model.Customer.CompanyName /div div Model.Customer.CustomerID /div div Html.Partial(CustomerListControl,Model.ProductList) /div/div 6 项目结构 7 运行结果 转载于:https://blog.51cto.com/12953214/1942283