MVC 2 - Display a jquery dialog that contains a partial view - asp.net-mvc

Okay, so I'm trying to display a dialog, and the contents of that dialog is from a partial view, _TestPartial.
Here is the partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div id="test" title="Basic dialog">
<p>This is just a test</p>
</div>
Now, if I put the test div in my .aspx that is displaying the dialog, it will show just fine. Or if I call the partial right from my page it will show 'This is just a test' just fine. I just have no idea how to make a jquery dialog display a partial. I've tried a few things, such as having my dialog call an Action, TestAction, which renders the _TestPartial, but that doesnt even work. I have a feeling I'm missing a key piece of code somewhere tho. Here is my dialog code(its called automatically based on a select list choice):
if ($('#SelectListID option:selected').text() == 'Test') {
$('#test').dialog({
width: 400,
resizable: false,
title: 'Test dialog',
open: function(event, ui) {
$(this).load("#Url.Action('TestAction')");
},
buttons: {
"Close": function (){
$(this).dialog("close");
}
}
});
}

I remember having a similar problem before. We added a dialog container and then loaded the partial view in an inner div.
<div id="dialogcontainer">
<div id="dialogcontent"></div>
</div>
$("#dialogcontainer").dialog({
width: 400,
resizable: false,
title: 'Test dialog',
open: function(event, ui) {
$("#dialogcontent").load("#Url.Action('TestAction')");
},
buttons: {
"Close": function (){
$(this).dialog("close");
}
}
});

Related

how do you open a jquery ui dialog window in parent/opener window?

I am trying to get a jquery ui dialog to open in a parent window from a child window. In other words, as the child can only have the dialog popup only with in its window's constraints, I would like to be able to use the contents of the dialog's popup in the constraints of the parent window.
I was trying to use a window.opener to tie in a link, but wasn't having much luck.
Here's the code that opens the window from a Backbone View HTML page:
popoutWindow = window.open("campaign/genericPopout.aspx", "search", "width=800, height=600, toolbar=0, status=0, menubar=0, scrollbars=1, resizable=1", true);
inside the genericPopout.apsx's call to a file that builds, with Backbone View, with this command,
new popupSearchView();
it calls the following Backbone View:
var popupSearchView = Backbone.View.extend({
events: {
"click #closeBtn" : "closePopupSerachView"
},
initialize: function() {
this.template = _.template("<div id='searchPopupView'>" +
"<div style='width: 100%; text-align: center;'>Hello World</div>" +
"<div style='text-align: center;'><input type='button' id='closeBtn' value='Close' /></div>" +
"</div>"
);
this.render();
},
render: function () {
formString = this.template();
$(formString).dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
})
return this;
},
closePopupSearchView: function () {
$(this).dialog("close");
}
});
It opens the popup dialog fine within the newly opened window, but it doesn't seem to work. In the parent window HTML, I have a div devoted to this dialog:
<div id="popupSearchViewDiv"></div>
and I tried setting the Backbone el to this div by this:
el: $("popupSearchViewDiv", window.opener.document)
yet this does not seem to work. Any thoughts on how I can accomplish this?
Thank You
I came up with a way to do this. I'm not sure how efficient it is, but it may be the only way.
In the opener window, I placed the code jquery-ui dialog:
function openSearchViewPopup() {
$("#popupSearchViewDiv").dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
}
With a predefined DIV in the index.aspx of:
<div id="popupSearchViewDiv"></div>
From the window opened from the index.aspx that holds the above function code, I call the function like this:
render: function () {
var htmlToWrite = this.template();
$(window.opener.docuemnt.getElementById("popupSearchViewDiv").append(htmlToWrite);
$window.opener.openSearchViewPopup();
}
This seems to allow the jquery-ui dialog popup to open in the window.opener window and to be used there.

MVC 5 partial view as jQuery dialog using knockout

I am using knockout to render a page with rows of data. On each row there is a link which should call a controller function which returns a partial view.
knockout script for link is (inside foreach loop)...
<a class="lnkEdit" data-bind="attr: {'href': '#Url.Action("ControllerActionName", new RouteValueDictionary() { { "Controller", "ControllerName" } })/'+ id}">Details</a>
Script section...
$(document).ready(function () {
$.ajaxSetup({ cache: false });
$("#dialog-edit").dialog({
title: 'Details',
autoOpen: false,
resizable: false,
position: ['center', 50],
width: 700,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(".ui-dialog-titlebar-close").hide();
$(this).load(url);
}
});
$(".lnkEdit").on("click", function (e) {
url = $(this).attr('href');
$("#dialog-edit").dialog('open');
return false;
});
$("#btncancel").on("click", function (e) {
$("#dialog-edit").dialog("close");
return false;
});
ko.applyBindings(new UnitViewModel());
})
My page has div as place holder for dialog...
<div id="dialog-edit" style="display: none">
Problem: When I click on link for details; the controller returns partial view but jquery is not able to generate dialog so the page opens as normal view. What is wrong with this?
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
I'll Check Your code and is working...
just add CSS and JS proper.

Append jQuery UI dialog to its parent

I have the following html:
<ul id="sortable">
<li class="ui-state-default">1
<div class="dialog-modal" title="Animal Facts" style="display:none;">
<p>What is the fastest animal on Earth?</p>
</div>
</li>
<li class="ui-state-default">2
<div class="dialog-modal" title="Animal Facts" style="display:none;">
<p>What is the largest animal on Earth?</p>
</div></li>
​
and the following jQuery code:
$( ".dialog-modal" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true
});
$('.ui-state-default').click(function() {
$(this).find("div").dialog( "open" );
});
This does not open the modal dialog on click, what am I missing?
Thanks for the help.
This is a current behavior of the jQuery UI dialog.
When defined the jQuery UI dialog is created and appended to the body.
The workaround solution is to append the dialog to its original parent after the creation like:
$(function () {
$(".dialog-modal").each(function (index) {
var origContainer = $(this).parent();
$(this).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
create: function () {
$(this).closest('div.ui-dialog')
.click(function (e) {
e.stopPropagation();
});
}
}).parent().appendTo(origContainer);
});
$('.ui-state-default').click(function (event) {
$(this).find("div").find(".dialog-modal").dialog("open");
});
});
An important note: look at the create event defined; it's necessary because, the open dialog method executed on the .ui-state-default class elements click is triggered on every click inside the dialog! It's formally correct because now the dialog is contained inside its parent and the click is propagated to the parents with .ui-state-default class. With stopPropagation the problem is solved.
It's a bit hackish, but it works.
Working fiddle: http://jsfiddle.net/IrvinDominin/Avtg9/8/
With the future version of jQuery UI 1.10.0 will be added the option appendTo, to handle this situation without any workaround http://api.jqueryui.com/dialog/#option-appendTo

jquery ui dialog is creating two dialogs

A screenshot : http://d.pr/i/A4Kv
This is my dialog code:
function popupbox(title,html,buttonTxt,buttonAction) {
var buttons = {};
if(buttonTxt != null) {
buttons[buttonTxt] = buttonAction;
}
buttons['Cancel'] = function() {
jQuery(this).dialog('destroy').remove();
};
var p = jQuery('<form class="dialoginnerbox">' + html + '</form>');
p.dialog({
autoOpen: false,
resizable: false,
modal: false,
width: 'auto',
height: 'auto',
maxHeight: 600,
maxWidth: 980,
title: title,
close: function(event, ui){
jQuery(this).dialog('destroy').remove();
},
buttons: buttons
});
p.dialog('open');
}
Any ideas?
---- UPDATE ----
I swapped out the returning html for some dummy text and that fixed it.. so something with the html that is being put into the popup is making it open twice...
Malformed html and inline script tags cause jquery ui dialog to open multiple dialogs.
With jQueryUI dialogs; jQuery may consider valid html as malformed html in some cases. The reason I say this is I ajax loaded the html for my dialog with valid html and valid html comments. I got double dialog boxes until I removed the html comments from the ajax loaded html. Example...
content.htm
<div id="myDialogContent">Alert!</div><!-- Here is an innocent looking comment -->
dialog.js
$.get( '/content.htm', function( html ){
$( html ).dialog();
});
This would produce a double dialog. If the html begins or ends with an html comment, the same dialog issue occurs. The only way around is to either remove the html comment or wrap the html text in another html tag like so...
dialog.js
$.get( '/content.htm', function( html ){
$( '<div>'+html+'</div>' ).dialog();
});
This would produce one dialog.
function display_dialog() { if($('.dialog_wrapper').length) {
return;
}
$('<div class="dialog_wrapper"</div>').dialog({
autoOpen: true,
modal: true,
A workaround for this problem is to use a counter:
var count = 0;
var $dialog = $('<div></div>') .dialog({ ...
autoOpen: false,
modal: true,
height: 625,
width: 500,
title: pagetitle
...
});
if (count > 0) {
$dialog.dialog("destroy").remove();
count = 0;
}
$dialog.dialog('open');
count++;
worked for me... for the dialog issue, but i am getting multiple requests on the server ...
something like that: when i click for the first time on the link, it sends on information to the server. When i click for the second time (without refreshing the browser) it sends the same information twice. When i click for the third time, three requests are sent to the server, and so on...
If you wrap your html in a single tag, it may clean it up.
The inclusion of divs inside the html may cause a dialog for each one unless there's a layer above:
This didn't work for me:
<hr /> blah blah blah
<h3>blahblahtitle</h3>
<div id=somestufffirst>here's the stuff</div>
<div id=someotherstuff>here's some more stuff</div>
But this did:
<div>
<hr /> blah blah blah
<h3>blahblahtitle</h3>
<div id=somestufffirst>here's the stuff</div>
<div id=someotherstuff>here's some more stuff</div>
</div>

Issues adding a class to jquery ui dialog

I'm trying to add an additional class to my jQuery dialog with the dialogClass property. Here's the javascript:
$(function(){
$( "#toogleMAmaximized" ).dialog({
title: 'Missions and Achivments',
autoOpen: false,
height: 500,
width: 700,
modal: true,
dialogClass: 'noPadding',
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
})
$( "#toogleMAminimized" ).click(function() {
$( "#toogleMAmaximized" ).dialog( "open" );
$( "#toogleMAmaximized" ).dialog({dialogClass:'noPadding'});
});
})
<div id="toogleMAminimized" style="" class="noPadding">
<div class="" style="cursor: pointer;position: absolute;right: 0;top: 45px;"><img src ="images/MAminimized.png" alt="missions and achivments"/></div>
</div>
Just in case you need it, my html code
<div id="toogleMAmaximized" >
<div id="missions">
<div id="mission1" missiontitle="A new home!" missionpoint="1" missionicon="images/missions/icon/anewhome-icon.png" missionimage="images/missions/anewhome.png" made="f" class="mission notDone"> </div>
</div>
<div id="achivments">
<div id="achivment1" achivmenttitle="Lucha sin cuartel!" achivmentpoint="10" achivmenticon="images/achivments/icon/1.png" achivmentimage="images/achivments/icon/luchasincuartel-plata-ico.png" made="t" class="achivment done"> </div>
</div>
</div>
As you can see, I've tried to add the class in many ways, I've tried all possible combinations but keep getting the same result: no noPadding class
Your noPadding class is being added successfully to the dialog. I have confirmed this by placing your markup and scripts within a fiddle, and loading jQuery UI 1.8.16 (the version you were testing with). This test is available online at http://jsfiddle.net/QHJKm/3/.
I suspect the confusion here is with the expected effect noPadding is going to have on the dialog itself. It could be that you interpreted its lack of effect as an indication it wasn't added to begin with. As you'll note in my example, I've got with a rather bold style, a red background. This quickly confirms that the class is indeed being added to the dialog.

Resources