zoukankan      html  css  js  c++  java
  • Mvc 刷新PartialView

        本文描述如何重新加载整个PartialView。

        Controller:

        

    public class HomeController : Controller
        {
            public static int i = 0;
            /// <summary>
            ///这个数组是超过来的,懒的改了。
            /// </summary>
            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";
    }
    <script type="text/javascript">
        function BtnClick() {
    
            //注意这一行,是针对的,后台Action中没有设置页面缓存机制的情况的。随机产生一个数就是,为了让页面加载的时候不在缓存中读。
            //$('#quote').load('/home/quote/' + Math.random());
            $('#quote').load('/home/quote/');
    
        }
    </script>
    <p>
        <input type="button" onclick="BtnClick()" value="刷新局部页" />
        <div id="quote">
        </div>
    </p>

        _Quote.cshtml:

        

    <h3>@ViewBag.Quote</h3>

         主要的思路是:

          1.在主页面上定义个一div。

          2.点击按钮(或者其他动作),触发div的load事件。

          3.在前端js或者后台Action中避免页面缓存。

          4.页面加载。

        

  • 相关阅读:
    TMD 这个写笔记的号,盗了有意思吗
    类成员的指针必须NULL化,否则是乱七八糟的东西
    超前引用不可使用类名来定义变量和函数的变量参数,只可用来定义引用或者指针。
    XP下,移动窗口产生重影的问题
    生成ico格式图标
    设置窗口的z-order总是在最底部
    关于windows的锁定状态
    使用Layered Window遇到的一些问题及解决方法
    转-使用wifi调试程序
    URL的格式
  • 原文地址:https://www.cnblogs.com/glorysword/p/2801347.html
Copyright © 2011-2022 走看看