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" οnclick="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/archive/2012/12/04/2801347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值