本文描述如何重新加载整个PartialView。
Controller:
public class HomeController : Controller { public static int i = 0; //////这个数组是超过来的,懒的改了。 /// public static string[] quotes = { "The first 90% of the code accounts for the first 90% of the development time.","The remaining 10% of the code accounts for the other 90% of the development time", "In order to understand recursion, one must first understand recursion", "I have always wished for my computer to be as easy to use as my telephone;", "my wish has come true because I can no longer figure out how to use my telephone.", "The gap between theory and practice is not as wide in theory as it is in practice.", "Nine people can’t make a baby in a month" }; public ActionResult Index() { return View(); } [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 1)] public ActionResult Quote() { //注释这个地方,原本是随机生成数组的index,结果上来我就悲剧, 连续几次都是用一个数,想了想为了效果明显还是改成其他的吧。 //var r = new Random(); //var rv = r.Next(0, 4); if (i == 6) { i = 0; } ViewBag.Quote = quotes[++i]; return PartialView("_Quote"); } }
Index页面:
@{ ViewBag.Title = "Home Page";}
_Quote.cshtml:
@ViewBag.Quote
主要的思路是:
1.在主页面上定义个一div。
2.点击按钮(或者其他动作),触发div的load事件。
3.在前端js或者后台Action中避免页面缓存。
4.页面加载。