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

    

原文地址:https://www.cnblogs.com/glorysword/p/2801347.html