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.页面加载。

        

  • 相关阅读:
    改进昨天的电梯电梯问题
    电梯调度
    结对开发四---实现环状二维数组求最大子数组的和
    结对开发五--对一千个数long型的一维数组求最大子数组的和
    结对开发四------求一维无头数组最大子数组的和
    结对开发3--电梯调度问题需求分析
    SQL基础题
    类和对象(课后题)
    再战文件(小甲鱼课后题)python超级入门
    快来集合!python超级入门笔记
  • 原文地址:https://www.cnblogs.com/glorysword/p/2801347.html
Copyright © 2011-2022 走看看