【问题标题】:Refresh page with PartialView that was loaded the last time使用上次加载的 PartialView 刷新页面
【发布时间】:2020-12-02 22:16:55
【问题描述】:

我正在使用ASP.NET Core。 在我的一个页面中,我有一个 div,可以将不同的 PartialView 加载到其中

<html>
<body style="background:url('/images/bg.png')">
    <div class="text-center">
        <div class="container" style="overflow-x:auto; background-color:white">
            <input type="button" class="btn btn-primary" value="Patients" onclick="LoadPartial('recipe')" />
            <input type="button" class="btn btn-primary" value="Orders" onclick="LoadPartial('orders')" />
            <input type="button" class="btn btn-primary" value="Delivered" onclick="LoadPartial('deliveries')" />
            <div id="mypage">
                <partial name="RecipeList.cshtml" />
            </div>
        </div>
        </div>
        <script>
        function LoadPartial(type) {
            var url = '@Url.Action("Recipes", "Glasses")';
            if (type == 'recipe')
                url = '@Url.Action("Recipes", "Glasses")';
            if (type == 'orders')
                url = '@Url.Action("Orders", "Glasses")';
            if (type == 'deliveries')
                url = '@Url.Action("Deliveries", "Glasses")';

            $('#mypage').load(url);
        }

        </script>
</body>

当用户第一次打开这个页面时,它应该加载partialview,就像第一个按钮被点击一样。

当另一个partialview 被加载并且我点击浏览器上的刷新按钮时,我想刷新一个不是第一个加载的partiaview。

我该怎么做?

【问题讨论】:

    标签: asp.net-core-mvc partial-views razor-pages


    【解决方案1】:

    您可以在单击按钮时将 url 存储在 localStorage 中,并在 $(document).ready() 函数中从中获取值,然后加载部分视图。

    <body>
        <div class="text-center">
            <div class="container" style="overflow-x:auto; background-color:white">
                <input type="button" class="btn btn-primary" value="Patients" onclick="LoadPartial('recipe')" />
                <input type="button" class="btn btn-primary" value="Orders" onclick="LoadPartial('orders')" />
                <input type="button" class="btn btn-primary" value="Delivered" onclick="LoadPartial('deliveries')" />
                <div id="mypage">
                </div>
            </div>
        </div>
    
    </body>
    
    @section scripts{ 
        <script>
            $(document).ready(function () {
                var url = localStorage.getItem('url');
                $('#mypage').load(url);
            })
    
    
            function LoadPartial(type) {
                var url = '@Url.Action("Recipes", "Home")';
                if (type == 'recipe')
                    url = '@Url.Action("Recipes", "Home")';
                if (type == 'orders')
                    url = '@Url.Action("Orders", "Home")';
                if (type == 'deliveries')
                    url = '@Url.Action("Deliveries", "Home")';
                localStorage.setItem('url', url);
                $('#mypage').load(url);
            }
    
        </script>
    }
    

    结果:

    更新:

    使用会话存储:

    <body>
        <div class="text-center">
            <div class="container" style="overflow-x:auto; background-color:white">
                <input type="button" class="btn btn-primary" value="Patients" onclick="LoadPartial('recipe')" />
                <input type="button" class="btn btn-primary" value="Orders" onclick="LoadPartial('orders')" />
                <input type="button" class="btn btn-primary" value="Delivered" onclick="LoadPartial('deliveries')" />
                <div id="mypage">
                    
                </div>
            </div>
        </div>
    
    </body>
    
    @section scripts{ 
        <script>
            $(document).ready(function () {
                var orignalurl = '@Url.Action("Recipes", "Home")';
                var url = sessionStorage.getItem('url');
                if(url != null)
                    $('#mypage').load(url);
                else
                    $('#mypage').load(orignalurl);
            })
    
    
            function LoadPartial(type) {
                var url = '@Url.Action("Recipes", "Home")';
                if (type == 'recipe')
                    url = '@Url.Action("Recipes", "Home")';
                if (type == 'orders')
                    url = '@Url.Action("Orders", "Home")';
                if (type == 'deliveries')
                    url = '@Url.Action("Deliveries", "Home")';
                sessionStorage.setItem('url', url);
                $('#mypage').load(url);
            }
    
        </script>
    }
    

    【讨论】:

    • 谢谢,但是当用户打开页面时,它应该在本地存储中加载第一个而不是最后一个
    • 你的意思是当用户第一次来到这个页面时它应该加载原始的部分视图?怎么用sessionStorage
    • 那么,你有没有试过我上面提到的sessionStorage?
    • 不,不幸的是我不知道如何使用它,你的意思是代替localStorage?你能解释更多吗?
    • 是啊,把localStorage换成sessionStorage,看看我的更新,也看看他的link
    猜你喜欢
    • 2011-10-22
    • 2020-11-08
    • 2013-12-25
    • 1970-01-01
    • 2021-06-10
    • 2021-01-05
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多