【问题标题】:How to load a partial view in an iframe on ajax call?如何在 ajax 调用的 iframe 中加载部分视图?
【发布时间】:2015-02-04 07:40:37
【问题描述】:

我正在开发 MVC 4 并尝试使用 $.ajax 调用在 iframe 中加载部分视图,如下所示:

$(document).ready(function () {

    $("#btnInsert").click(
    function(e) {
        e.preventDefault();
        var loc = window.location.href;
        loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Home" : loc;
        console.log(loc + "/Insert");

        $.ajax({
            type: "GET",
            url: loc+ "/Insert",
            success: function (msg) {
                console.log(msg);
                //$('FrameforPopUp.html').html(msg);
                //$("#ShowNextView").css(
                $("#ShowNextView").html(msg);
                //$("ShowNextView").src
                showView(msg);
                $("#ShowNextView").attr("disabled", "enabled");
                //if (msg.d)
                //    showView(msg.d);
                //else
                //    alert("Data is invalid")
            },
            error: function () {
                alert("An unexpected error has occurred during processing.");
            }
        });
    });

    function showView(resultView) {
        $("#ShowNextView").dialog({          //resultView
            modal: true,
            width: "auto",
            height: "auto",
            position: "center",
            resizable: false,
            closeOnEscape: true,
            open: function (ev, ui) {
            }
        });
    }

我的框架如下:

<iframe id="ShowNextView" style="visibility:hidden;">

但它没有显示带有弹出窗口的 iframe.. 我希望该 iframe 加载“插入”视图并在 btnInsert 单击上的 ajax 调用后将其显示在弹出窗口中。请帮忙?

【问题讨论】:

  • 是弹出窗口本身不起作用还是弹出窗口中没有 html?
  • 嗯,我想即使 iframe 也没有正确加载。加上根本没有弹出窗口

标签: javascript jquery ajax asp.net-mvc iframe


【解决方案1】:

这就是您可以将 HTML 内容写入 iframe 的方式。

$("#btnInsert").click(function (e) {
    // ...
    $.ajax({
        type: "GET",
        url: loc + "/Insert",
        success: function (msg) {
            showView(msg);
        },
        error: function () {
            alert("An unexpected error has occurred during processing.");
        }
    });

});

function showView(resultView) {
    $('<div>').append('<iframe id="ShowNextView"></iframe>').dialog({
        modal: true,
        width: "auto",
        height: "auto",
        position: "center",
        resizable: false,
        closeOnEscape: true,
        open: function (ev, ui) {
            $(this).find('#ShowNextView').contents().find('body').html(resultView);
        }
    });
}

并且还从 HTML 中删除 &lt;iframe id="ShowNextView" style="visibility:hidden;"&gt;。

演示: http://jsfiddle.net/ssqxyvjc/

【讨论】:

  • 不工作.. iframe 还没有显示任何内容。
  • 它在演示中有效,对吧?所以这意味着你可能没有正确加载数据。将alert(resultView) 放入showView,是否显示加载的内容?
  • 是的,它在警告框中显示了部分视图的所有内容
  • 检查是否在 $(this).find('#ShowNextView').contents().find('body') 中找到 iframe。
  • 我没听懂你.. 怎么知道?
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
相关资源
最近更新 更多