什么时候用partial viewview

我们知道,作为Action的响应,最常见的做法是Return View();也就是说,返回一个视图。但是如果我们某的操作只是要返回页面的一部分,典型的情况就是,在页面上实现局部的刷新功能。
实现局部刷新功能,以前我们是自己用javascript,解析json数据来实现的。下面有一个例子
Action里面的代码
/// &summary&
& & & & /// 返回某个照片目前的评论消息
& & & & /// &/summary&
& & & & /// &returns&&/returns&
& & & & //[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult Blog(string id)
& & var blogs=new[]{
new {Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now.Tostring()},
& & new {Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now.ToString()},
new {Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now.Tostring()},
& & new {Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now.ToString()},
new {Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now.Tostring()}},
return &Json(blogs,&text/json&);
在页面中的JavaScript代码
type: &POST&,
url: action+key,
dataType: &json&,
success:function(result){
$(&#blog&).empty();
var ol=$(&&ol /&&);
$.each(result,function(i,n){
var &t=n.Title+&,(& + n.Author +&),创建于:&+ n.Time + &&div&& + n.Details + &&/div&&;
$(&&li /&&).append(t).appendTo(ol);
ol.appendTo($(&#blog&));
这样做确实可以实现我们的功能,但太过繁琐,而且因为要在js中拼接那些div,实在是很容易出错的
更好的做法,首先创建一个PartialView,就是一个UserControl
&%@ &Control Language=&C#& Inherits=System.Web.Mvc.ViewUserControl&IEnumerable&web.Models.BlogItem&&%&
& & & & &tr&
& & & & & & &th&
& & & & & & & & Ttile
& & & & & & &/th&
& & & & & & &th&
& & & & & & & & Author
& & & & & & &/th&
& & & & & & &th&
& & & & & & & & Details
& & & & & & &/th&
& & & & & & &th&
& & & & & & & & Time
& & & & & & &/th&
& & & & &/tr&
& & &% foreach (var item in Model) { %&
& & & & &tr&
& & & & & & &td&
& & & & & & & & &%= Html.Encode(item.Title) %&
& & & & & & &/td&
& & & & & & &td&
& & & & & & & & &%= Html.Encode(item.Author) %&
& & & & & & &/td&
& & & & & & &td&
& & & & & & & & &%= Html.Encode(item.Details) %&
& & & & & & &/td&
& & & & & & &td&
& & & & & & & & &%= Html.Encode(item.Time) %&
& & & & & & &/td&
& & & & &/tr&& &&
& & &% } %&
& & &/table&
然后,修改一下Action的代码
& & & & /// &summary&
& & & & /// 返回某个照片目前的评论消息
& & & & /// &/summary&
& & & & /// &returns&&/returns&
& & & & //[AcceptVerbs(HttpVerbs.Post)]
& & & & [Authorize]
& & & & public ActionResult Blog(string id)
& & & & & & var blogs = new Models.BlogItem[]
& & & & & & {
& & & & & & new Models.BlogItem(){Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now},
& & & & & & new Models.BlogItem(){Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now},
& & & & & & new Models.BlogItem(){Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now},
& & & & & & new Models.BlogItem(){Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now},
& & & & & & new Models.BlogItem(){Title=&评论标题&,Details=&我的评论&,Author=&张三&,Time=DateTime.Now}
& & & & & & };
& & & & & & return PartialView(&BlogView&, blogs);
这样的话,js中就只要一句话了
& & & & & & $(&#blog&).load(action + key);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:35432次
排名:千里之外
原创:55篇
转载:30篇
(3)(8)(5)(1)(6)(1)(10)(1)(3)(3)(6)(1)(3)(19)(14)使用MVC框架中要注意的问题(六):何时使用PartialView方法 - 陈希章 - 博客园
我们知道,作为Action的响应,最常见的做法是Return View();也就是说,返回一个视图。但是如果我们某的操作只是要返回页面的一部分,典型的情况就是,在页面上实现局部的刷新功能。 实现局部刷新功能,以前我们是自己用javascript,解析json数据来实现的。下面有一个例子 Action里面的代码
/// &summary&
/// 返回某个照片目前的评论消息
/// &/summary&
/// &returns&&/returns&
//[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult Blog(string id)
var blogs = new[]{
new {Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now.ToString()},
new {Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now.ToString()},
new {Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now.ToString()},
new {Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now.ToString()},
new {Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now.ToString()}};
return Json(blogs, "text/json");
在页面中的javascript代码
type: "POST",
url: action + key,
dataType: "json",
success: function(result) {
$("#blog").empty();
var ol = $("&ol /&");
$.each(result, function(i, n) {
var t = n.Title + ",(" + n.Author + "),创建于:" + n.Time + "&div&" + n.Details + "&/div&";
$("&li /&").append(t).appendTo(ol);
ol.appendTo($("#blog"));
这样做确实可以实现我们的功能,但太过繁琐,而且因为要在js中拼接那些div,实在是很容易出错的。
更好的做法是,首先创建一个PartialView,其实就是一个UserControl&%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&IEnumerable&Web.Models.BlogItem&&" %&
&% foreach (var item in Model) { %&
&%= Html.Encode(item.Title) %&
&%= Html.Encode(item.Author) %&
&%= Html.Encode(item.Details) %&
&%= Html.Encode(item.Time) %&
然后,修改一下Action的代码
/// &summary&
/// 返回某个照片目前的评论消息
/// &/summary&
/// &returns&&/returns&
//[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult Blog(string id)
var blogs = new Models.BlogItem[]
new Models.BlogItem(){Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now},
new Models.BlogItem(){Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now},
new Models.BlogItem(){Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now},
new Models.BlogItem(){Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now},
new Models.BlogItem(){Title="评论标题",Details="我的评论",Author="陈希章",Time=DateTime.Now}
return PartialView("BlogView", blogs);
这样的话,js中就只要一句话了
$("#blog").load(action + key);MVC调用部分视图PartialView_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
MVC调用部分视图PartialView
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢最近在学习MVC布局的一些东西,在网上跟一些有经验的技术大神们学习了一些内容,再加上自己的一些实践,慢慢的也有了一点心得体会,下面将它记载下来,便于有需要的人也便于自己翻阅。另外,我的实践过程中选择的是Razor视图引擎。
一 MVC中的部分视图PartialView:
  MVC中的PartialView其实相当于用户控件ascx,其主要作用主要包括两方面:
  可复用性:如果视图中的一部分内容会在多个地方用到,那么有必要将它写成一个分部视图,这样就不必在每一个视图中都重新写一遍,而直接调用分部视图就可以了;
  便于维护:如果一个View有许多块组成,那么可以将这些块拆分成多个分部视图,这样就不必在一个Action中处理许多的数据或者是在一个View中写很多的html语言了,虽然这需要添加很多的cshtml文件或者Action,但是当某一块的数据发生变化就很容易管理和维护了,只需要对这个块的内容进行维护,而不必全局的去调整,这样一来岂不更显得有条理性?
二 分部视图的调用:
  在一个View中调用分部视图主要有下列四种方法:
@Html.Patial  
@Html.RenderPatial
@Html.Action
@Html.RenderAction
  例如调用Html.RenderPartial有四种方式:
  Html.RenderPartial(string partialName);
  Html.Renderpartial(string partialName,object model);
  Html.RenderPartial(string partialName,ViewDataDictionary viewData);
  Html.RenderPartial(string partialName,object model,ViewDataDictionary viewData);
  更加具体的方法详见MSDN:
三 Html.RenderPatial和Html.RenderAction的主要区别:    
  Html.RenderPatial:直接将View呈现在父视图的相应地方,View中绑定的Model需事先生成好
  Html.RenderAction:需要进入子视图对应的Action方法中,按照Controller-&Model-&View的顺序走一遍,然后将产生的View呈现在父视图相应的地方,如果要呈现的视图需要后台处理数据,那么需要选用Html.RenderAction或者Html.Action。
比较形象的描述:
四 Html.RenderPartial和Html.Partial以及Html.RenderAction和Html.Action的区别:
  Html.Partial和Html.Action都返回的是字符串,可以将它们的返回值赋给一个变量
  Html.RenderPartial和Html.RenderAction都返回void,而内容会在执行的时候直接写到Response中,
  这样一来在View中加载它们的方式也就不一样了:
  @Html.Partial("PartialViewName")
  @Html.Action("Action", "Controller")
  @{ Html.RenderPartial("PartialViewName");}
  @{ Html.RenderAction("Action", "Controller");}
  在使用中,关于Html.Partial和Html.RenderPartial以及Html.Action和Html.RenderAction具体选用哪一种,我也不清楚,但引用下面的一段话,希望有明白的大神们给出解释和补充,源文地址:
  我更多的时候会用 RenderPartial 和RenderAction 因为这两种方法在输出内容时使用的TextWriter就是当前Template所用的. 与此相反@Html.Partial 和@Html.Action 两种方法每次都会创建自己的 TextWriter 实例并且把内容缓存在内存中. 最后把所有 writer输出的内容发送到一个 MvcString对象中 所以它可以当成 unescaped HTML用@直接渲染.
  On the other side I do not entirely through away@Html.Partial and@Html.Action. These methods are good for some cases when you need to put sub-template content into a variable or do something with it (which was a pain in older MVC versions). And they may be life-saving in certain scenarios like caching.
  Oh there is also a@RenderPagemethod ofWebPagebasethat doesn&t use MVC template lookup and receives exact template path as its parameter. But anyway I would use HtmlHelper&s Partial instead as it is a more traditional view-engine independent approach for rendering child templates.It is true that the result is the same. Html.Partial returns the partial view as a string so basically you can stil do anything with the rendered content before you emit it. If you use RenderPartial you are directly writing to the output stream and because of this RenderPartial will be a bit faster.
阅读(...) 评论()& & & 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")
如何实现?
&&&using S
using System.Collections.G
using System.L
using System.W
namespace Step1.Models
& & public class Product
& & & & public string Name
& & & & & &
& & & & & &
& & & & public string Banner
& & & & & &
& & & & & &
using System.Collections.G
using System.L
using System.W
namespace Step1.Models
& & public class OrderModel
& & & & public Customer Customer
& & & & & &
& & & & & &
& & & & public List&Product& ProductList
& & & & & &
& & & & & &
using System.Collections.G
using System.L
using System.W
using Step1.DAL;
using Step1.M
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);
& & & & & & }
& & & & & &
3 &Controller
using System.Collections.G
using System.L
using System.W
using System.Web.M
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.M
@using System.C
@model IEnumerable&Product&
&table border="1" &
& & & & &td&
& & & & & & Name
& & & & &/td&
& & & & &td&
& & & & & & Banner
& & & & &/td&
& & @foreach (var item in Model)
& & & & &tr&
& & & & & & &td&
& & & & & & & & @item.Name
& & & & & & &/td&
& & & & & & &td&
& & & & & & & & @item.Banner
& & & & & & &/td&
& & & & &/tr&
using&Step1.M@model&OrderModel@{&&&&ViewBag.Title&=&"Index";}&h2&Index&/h2&&div&&&&&&div&&&&&&&&&@panyName&&&&&/div&&&&&&div&&&&&&&&&@Model.Customer.CustomerID&&&&&/div&&&&&&div&&&&&&&&&@Html.Partial("CustomerListControl")&&&&&/div&&/div&
6 项目结构
7 运行结果
阅读(...) 评论()

我要回帖

更多关于 return partialview 的文章

 

随机推荐