简体   繁体   English

MVC 3 Razor弹出窗口

[英]MVC 3 Razor PopUp Window

I need to open a new pop up window on click of a button in a view. 单击视图中的按钮后,我需要打开一个新的弹出窗口。 The new window should be redirect to a specific actionmethod in a specific controller. 新窗口应重定向到特定控制器中的特定操作方法。 I also need to provide attributes for size of the new pop up window. 我还需要提供新弹出窗口大小的属性。 I have been trying the following code: 我一直在尝试以下代码:

<input type="button" name = "ClickMe" Value="ClickMe" onclick= "javascript:window.open('/Home/Create/','Customer Search',height='window.screen.height - 100', width='200',left='window.screen.width - 250' ,top='10',status='no',toobar='no',resizable='yes',scrollbars='yes')"/>

On click of button, nothing happens. 单击按钮,什么也没有发生。 I get following Javascript error: 我收到以下Javascript错误:

Line: 19 行:19
Char: 1 字符:1
Error: Invalid argument. 错误:参数无效。
Code: 0 代码:0

When I check the ViewSource of the HTML rendered, I find the line to be the one which is rendering the button. 当我检查呈现的HTML的ViewSource时,发现该行是呈现按钮的那一行。 I am using Windows Vista with IE 7. I am working on MVC 3 with Razor Engine in VS 2010 我正在将Windows Vista与IE 7配合使用。我正在VS 2010中使用Razor Engine处理MVC 3。

Respect html. 尊重html。 Respect javascript. 尊重javascript。 Respect the framework that you are writing on, that has made two big changes (validation and ajaxity) from its 2nd version to 3rd to apply the newer, modern principle - Unobtrusive Javascript . 尊重您所写的框架,该框架从其第二版到第三版进行了两大更改(验证和Ajaxity),以应用更新的现代原理-Unobtrusive Javascript You could manage to correct that error in less time you spent on asking question in here if you followed that principle (with the help of vs javascript synthax highlighting). 如果您遵循该原则,则可以用更少的时间在此处提问,从而纠正该错误(借助vs javascript synthax高亮显示)。

    <input type="button" id="ClickMe" name = "ClickMe" Value="ClickMe" />
...
    <script type="text/javascript">
            $(function () {
                $('#ClickMe').click(function () {
                   window.open('/Home/Create/', 'CustomerSearch', 'height=' + (window.screen.height - 100) + ',width=200,left=' + (window.screen.width - 250) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                });
            });
    </script>

And as I discovered, it's the issue with space in window name in IE - 'Customer Search'. 正如我所发现的,这就是IE中“客户搜索”中窗口名称中空格的问题。 If you remove that space - 'CustomerSearch', it'll start working in IE too 如果您删除该空间-“ CustomerSearch”,它也将在IE中开始工作

The HTML provided has some quirks on the ' characters in the onclick . 提供的HTML对onclick'字符有一些怪异。 Try and edit to the following (linebreaks added for readability): 尝试编辑以下内容(添加了换行符以提高可读性):

<input type="button"
       name="ClickMe"
       value="ClickMe"
       onclick="javascript:window.open('/Home/Create/',
                                       'Customer Search',
                                       'height=' + (window.screen.height - 100) + ',width=200,left=' + (window.screen.width - 250) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');"/>

Notable changes: 显着变化:

  • Third argument to window.open() is one JavaScript string with values from calculations inserted window.open()第三个参数是一个 JavaScript字符串, 其中插入了来自计算的值
  • Arguments within the config string has ' removed 配置字符串中'参数已被删除
  • toobartoolbar . toobartoolbar

Based on archil's update it appears that he has hit the nail: 根据archil的更新 ,看来他已触手可及

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

strWindowName
This is the string that just names the new window. 这是仅命名新窗口的字符串。 Such string can be used to be the target of links and forms when the target attribute of an <a> element or of a <form> is specified. 当指定<a>元素或<form>的目标属性时,此类字符串可以用作链接和表单的目标。 This string parameter should not contain any blank space. 此字符串参数不应包含任何空格。 strWindowName does not specify the title of the new window. strWindowName不指定新窗口的标题。 ( source ) 来源

<script type="text/javascript">
    //script for loading value to division
    $(function () {
        $('form').submit(function () {
            $.ajax({
                url: this.action, //you can redirect to your specific controller here..
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#popUp').html(result);
                }
            });
            return false;
        });
    });
</script>

<script type="text/javascript">
    //Script for pop up dialog.
    $(function () {
        $('form').submit(function () {
            $("#popUp").dialog({
                modal: true,
                resizable: false
            });
        });
    });
</script>

<div id="popUp" >
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM