asp.net-mvc MVC-弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4803424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:47:44  来源:igfitidea点击:

MVC-pop up windows

asp.net-mvcpopup

提问by Cipiripi

I need to create pop up windows in mvc(not new tab in browser). Does anyone know how to do this?

我需要在 mvc 中创建弹出窗口(不是浏览器中的新选项卡)。有谁知道如何做到这一点?

回答by uvita

One possibility is to use jquery ui dialog.

一种可能性是使用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动作。该操作的结果 (html) 放置在弹出窗口的容器内,并在您打开弹出窗口的 ajax 调用的成功处理程序上。下面是一些示例代码:

@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!

祝你好运!

回答by Igor V Savchenko

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)

最明显的方法是使用 js 框架之一。我个人更喜欢 jQuery UI 对话框控件。有关它的详细信息,
请查看http://jqueryui.com/demos/dialog/
您也可以检查ASP.NET MVC 模态对话框/弹出窗口最佳实践(它的问题与您的相似)

Of course if you need some simple popup you always may use alert('Im popup');

当然,如果您需要一些简单的弹出窗口,您可以随时使用 alert('Im popup');

Update according your latest request
To open some url in new window you may use next javascript:

根据您的最新请求更新
要在新窗口中打开一些网址,您可以使用下一个 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

但结果真的取决于浏览器。他们中的大多数人不是在新选项卡中而是在新浏览器实例中打开此模式窗口。
本主题也可能对您有所帮助:
JavaScript 在新窗口中打开,而不是选项卡