[英]How to load a partial view in an iframe on ajax call?
I am working on MVC 4 and trying to load a partial view in an iframe using $.ajax call like this : 我正在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) {
}
});
}
and my frame is as follows : 我的框架如下:
<iframe id="ShowNextView" style="visibility:hidden;">
but it is not showing iframe with a pop up.. I want that iframe to load the "Insert" view and show it in pop up after the ajax call on the btnInsert click. 但它不显示带弹出窗口的iframe。我希望该iframe加载“插入”视图,并在btnInsert单击上的ajax调用后以弹出窗口显示它。 Please help?
请帮忙?
This is how you can write HTML content into iframe. 这是将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);
}
});
}
And also remove <iframe id="ShowNextView" style="visibility:hidden;">
from HTML. 并从HTML中删除
<iframe id="ShowNextView" style="visibility:hidden;">
。
Demo: http://jsfiddle.net/ssqxyvjc/ 演示: http : //jsfiddle.net/ssqxyvjc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.