使用MVC框架中要注意的问题(六):何时使用PartialView方法
来源:互联网 发布:手机精美时钟软件 编辑:程序博客网 时间:2024/06/08 05:54
我们知道,作为Action的响应,最常见的做法是Return View();也就是说,返回一个视图。但是如果我们某的操作只是要返回页面的一部分,典型的情况就是,在页面上实现局部的刷新功能。
实现局部刷新功能,以前我们是自己用javascript,解析json数据来实现的。下面有一个例子
Action里面的代码
////// 返回某个照片目前的评论消息 /// /// //[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"); }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
在页面中的javascript代码
$.ajax({ type: "POST", url: action + key, dataType: "json", success: function(result) { $("#blog").empty(); var ol = $(""); $.each(result, function(i, n) { var t = n.Title + ",(" + n.Author + "),创建于:" + n.Time + "
" + n.Details + ""; $("").append(t).appendTo(ol); }); ol.appendTo($("#blog")); } });
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
这样做确实可以实现我们的功能,但太过繁琐,而且因为要在js中拼接那些div,实在是很容易出错的。
更好的做法是,首先创建一个PartialView,其实就是一个UserControl
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl>" %> <table> <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的代码
////// 返回某个照片目前的评论消息 /// /// //[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); }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
这样的话,js中就只要一句话了
$("#blog").load(action + key);
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题:何时使用PartialView(部分类)方法
- MVC中使用PartialView方法
- ASP.NET MVC中使用PartialView方法
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- ASP.NET MVC中如何使用PartialView
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(一):修改首页以支持主题
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题:如何在页面和用户控件之间传递数据
- 15 Amazing jQuery Image Gallery/Slideshow Plugins and Tutorials
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 实现SQLServer的Session
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- Uninstalling ASP.NET MVC 1.1 after installing Visual Studio 2010 beta 2
- Enabling .NET 3.5 in SharePoint 2007 Sites, the Lazy Way{转载}
- 如何在javascript中访问页面定义的变量
- Required Content Placeholders in SharePoint Masterpages
- jquery ui中 accordion的问题及我的解决方法
- javascript中类的定义和使用{转载}