如何加载一个视图

如何加载一个视图

本文介绍了如何加载一个视图,然后在它使用JavaScript一个MVC项目的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我做什么,我不断收到相反的顺序。我只是想刷新整个页面的ajax完成一定的操作,然后只需加载一定的局部视图之后,这是我的尝试:

Whatever I do, I keep getting the reverse order. I just want to refresh the entire page after ajax finishes a certain action and then simply load a certain partial view, this is my attempt:

$.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: '@Url.Action("DeleteSensor", "PredefinedViews")',
                    data: JSON.stringify({ pviID: PredefineViewsItemID, pID: PredefineViewID }),
                    dataType: "json",
                    complete: function (result) {
                        location.reload(true);
                    },
                    success: function (result) {;
                        var urlShowSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefID" })";
                        urlShowSensors = urlShowSensors.replace("PredefID", PredefineViewID);
                        $(divSensorNames).load(urlShowSensors);

在调试我看到它成功加载的局部视图,但它最终做location.reload(真正的);最后这又使我有没有一个开放的局部视图一个漂亮的视野。 有没有一种方法,我可以先让它做location.reload(真正的);然后加载我的部分观点?

In the debug I see that it successfully loads the partial view, but then it ends up doing the location.reload(true); LASTLY which in turn leaves me with an open view without an open partial view. Is there a way I can FIRST make it do location.reload(true); and THEN load my partial view?

更新!看来我从来没有把的document.ready在我的脚本标记,这是我的整个脚本:

UPDATE!It appears I never put a document.ready in my script tags, here is my entire script:

<script type="text/javascript">
    $(function () {
        $(".sensor-delete-table").on("click", function () {
            var divSensorNames = $("#sensorNames");
            var tr = $(this).parents('tr:first');
            var PredefineViewsItemID = tr.find("#PredefineViewsItemID").html();
            var PredefineViewID = tr.find("#PredefineViewID").html();
            var amount = parseInt($("[data-id='" + PredefineViewID + "']").text());
            var flag = confirm('@Html.Localize("deleteCheck")');
            var urlShowNewSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefineViewID" })";
            urlShowNewSensors = urlShowNewSensors.replace("PredefineViewID", PredefineViewID);
            if (PredefineViewID != "" && flag) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: '@Url.Action("DeleteSensor", "PredefinedViews")',
                    data: JSON.stringify({ pviID: PredefineViewsItemID, pID: PredefineViewID }),
                    dataType: "json",
                    success: function (result) {
                        location.reload(true);
                    },
                    complete: function (result) {

                        var urlShowSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefID" })";
                        urlShowSensors = urlShowSensors.replace("PredefID", PredefineViewID);
                        $(divSensorNames).load(urlShowSensors);
                    },

                });
            }
        });
    });
</script>

我想我能做到这样,那么任何人都可以改正我的code吗?基本上,当我点击一个按钮,它会删除一个表中的一行在我的部分观点,并进入到一个控制器,它会删除数据库中记录。现在,如果一切顺利因此我想刷新我的主要观点这就是为什么我坚持做一个location.reload,然后我想我的加载局部视图,这就是为什么我需要一个网址上有一定div来加载的表

I thought I could do it like that, so can anyone correct my code here? Basically when i click a button, it deletes a row in a table in my partial view and it goes to a controller where it deletes the record from the data base. Now if everything went accordingly I want to refresh a table in my main view which is why I insist on doing a location.reload and then I want to load my partial view, which is why i need an url to be loaded on a certain div.

推荐答案

在加载功能的document.ready局部视图

Load the partial view in document.ready function

和放location.reload()在成功阻止

and put the location.reload() in success block

       $(document).ready(function()
       {
           var urlShowSensors = "@Url.Action("ShowSensorNames", "PredefinedViews",
                        new {                 predefinedViewID = "PredefID" })";
                    urlShowSensors = urlShowSensors.replace("PredefID",
                                                PredefineViewID);
                    $(divSensorNames).load(urlShowSensors);
       });


       $(".sensor-delete-table").on("click", function () {
       $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("DeleteSensor", "PredefinedViews")',
                data: JSON.stringify({ pviID: PredefineViewsItemID, pID: PredefineViewID
                    }   ),
                dataType: "json",
                success: function (result) {
                             location.reload(true);}});
       });

这篇关于如何加载一个视图,然后在它使用JavaScript一个MVC项目的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:50