MVC-pop up windows
Solution 1
One possibility is to use jquery ui dialog.
EDIT
The idea would be to have an ajax action that returns a partial view. The result of that action (html) is placed inside the container of the popup and on the success handler of the ajax call you open the popup. Here is some sample code:
@Ajax.ActionLink("Open popup", "SomeAction", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "result", InsertionMode = InsertionMode.Replace, OnSuccess="openPopup" })<br />
<div id="result" style="display:none;"></div>
<script type="text/javascript">
$(document).ready(function() {
$("#result").dialog({
autoOpen: false,
title: 'Title',
width: 500,
height: 'auto',
modal: true
});
});
function openPopup() {
$("#result").dialog("open");
}
</script>
Then you have to add the action in the controller that returns the partial view
[HttpGet]
public PartialViewResult SomeAction()
{
return PartialView();
}
Place whatever you need in the partial view, you may also include parameters in the action, etc.
Good luck!
Solution 2
Most obvious way is using one of js frameworks. Personally I prefere jQuery UI dialog control.
Please check http://jqueryui.com/demos/dialog/ for detailed information about it.
Also you may check ASP.NET MVC modal dialog/popup best practice (it's question similar to yours)
Of course if you need some simple popup you always may use alert('Im popup');
Update according your latest request
To open some url in new window you may use next javascript:
function OpenDialog() {
window.open("some url", "DialogName", "height=200,width=200,modal=yes,alwaysRaised=yes");
}
But result really depends on browser. Most of them open this modal window not in new tab but in new browser instance.
This topic might help you aswell:
JavaScript open in a new window, not tab
Related videos on Youtube
Cipiripi
Updated on June 25, 2020Comments
-
Cipiripi over 3 years
I need to create pop up windows in mvc(not new tab in browser). Does anyone know how to do this?
-
Cipiripi almost 13 yearsI need to pass url address that will be opened in the modal pop up, and I'm not sure how to do that with jquery..Any idea?
-
Igor V Savchenko almost 13 yearsIt opens just some div as a dialog content doesn't it? But how to open some other page in dialog?
-
uvita almost 13 yearsThat code worked for me (using asp.net mvc 3 rtm). You can specify what you want to show in the popup by specifying the name of the action and the controller in the AjaxActionLink parameters. The framework creates the url for you according to your routing configuration.
-
Cipiripi almost 13 yearsIt works for me, too. I just had to delete style="display:none;, because pop up was invisible
-
Gorgsenegger over 10 yearsDidn't work for me to begin with until I added
jquery.unobtrusive-ajax.js
(maybe others knew that, but I, being a newbie in that regard, didn't...). -
TechDo over 10 years@Gorgsenegger Please send the link to download
jquery.unobtrusive-ajax.js
. It's not working for me :( Does it work in MVC 2.0? -
Gorgsenegger over 10 years@techdo: You can find it via Google or download the
.nupkg
file form here nuget.org/api/v2/package/jQuery.Ajax.Unobtrusive (change the file's extension to .zip and open it to extract the script file). I don't know about MVC 2, I used it in MVC 3. -
TechDo over 10 yearsOkay @Gorgsenegger Thanks for replying... I will check.
-
Nick L Scott over 9 yearsI'm trying this approach but it stills open a new tab. Do I have to do something else?
-
Dnyati almost 4 yearssame here opening in new tab. Anything else need to add?