在ASP.NET MVC 4视图中将数据从父窗口传递到弹出(子)窗口

时间:2018-03-07 13:34:19

标签: javascript c# jquery html asp.net-mvc

目前,我被要求使用ASP.NET MVC构建表单,其中要求是:

  1. 表单中包含用于插入姓名,性别和出生日期等数据的文本框。
  2. 点击提交按钮后,会出现一个弹出窗口。
  3. 父窗口窗体中的所有数据都传输到弹出窗口(子窗口),并且父窗口的视图完全相同。
  4. 父窗口和弹出窗口之间的区别在于,弹出窗口中的文本框必须被禁用,同时仍然保留父窗口中的数据值,而且,我必须在弹出窗口中删除“查看”按钮
  5. 父窗口和弹出窗口都必须有只有一个CSHTML文件。(例如,不允许使用parent.cshtml和popup.cshtml)
  6. 以下是一些图片来说明我的需求:

    预期观点: Image 1

    这是我的代码

    //Centering the popup window, Code from https://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
      function popUp(url, title, w, h) {
        var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
        var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
    
        var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
        var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
    
        var left = ((width / 2) - (w / 2)) + dualScreenLeft;
        var top = ((height / 2) - (h / 2)) + dualScreenTop;
        var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
    
        // Puts focus on the newWindow
        if (window.focus) {
          newWindow.focus();
        }
      } 
    <form method="post">
      <div>
        Name:
        <input type="text" name="textName" id="txt" value="" />
      </div>
      <div>
        Gender:
        <input type="radio" name="gender" value="female" /> Male
        <input type="radio" name="gender" value="male" /> Female
      </div>
      <div>
        DOB:
        <input type="date" name="tanggal" id="txtDate" />
    
      </div>
      <div id="divSubmit">
        <input type="submit" value="View" id="submit" onclick="popUp('Index.html','Index','900','500'); return false;" />
      </div>
    </form>

    这是以上代码的结果视图:

    结果视图:Image 2

    非常感谢任何建议和帮助!

1 个答案:

答案 0 :(得分:1)

您可以在window.open之后添加此代码。

newWindow.onload = function () {
    this.document.getElementById('txtPopup').value = document.getElementById('txt').value;
}

替换txtPopup,你必须输入弹出窗口输入的id。