简体   繁体   English

如何在加载时提交MVC视图?

[英]How do I submit a MVC view on load?

I have a view that has a form with a single hidden submit button. 我有一个带有单个隐藏提交按钮的表单的视图。 I want to submit the form when the page loads. 我想在页面加载时提交表单。 However, any submit I do in Javascript gives me this error: 但是,我用Javascript做的任何提交都会给我这个错误:

Unable to get property 'submit' of undefined or null reference 无法获取未定义或空引用的属性“提交”

Here is my code: 这是我的代码:

PrintApplication.cshtml PrintApplication.cshtml

    @using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
    {
      <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" />
    }

<script>

    document.onreadystatechange = function () {
        document.form.submit();
    }

</script>

In place of document.form.submit, I've also tried 我也尝试代替document.form.submit

var form = document.getElementById("PrintApplications");
form.submit();

document.forms["PrintApplications"].submit();

this.form.submit();

and other variations (with # in front of the form name, for example) 和其他变体(例如,表单名称前带有#)

What is wrong with my code. 我的代码有什么问题。 Is there any other way to submit the page automatically (without the user clicking any buttons)? 还有其他方法可以自动提交页面(无需用户单击任何按钮)?

Edit: Here's my original code. 编辑:这是我的原始代码。 It didn't work in IE which is what set off the problem I'm having. 它在IE中不起作用,这是我遇到的问题的原因。 There was no error, but the form didn't submit. 没有错误,但表单未提交。 This worked fine in Chrome though. 不过,这在Chrome浏览器中效果很好。

    $(document).ready(function () {
    $("#btnPrintApplications").click();
    });

Edit 2: Here's the code I got to work using the code in the post from @barry 编辑2:这是我要使用@barry帖子中的代码的代码

@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
{
    @*<input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" />*@
    <div>
        If applications do not appear in a separate tab or window, please click the button below.
    </div>
    <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" />
}

<script>
  mySubmitButton = document.getElementById("btnPrintApplications");
    mySubmitButton.click();
</script>

Edit 3: Here's the code using the example from @chris-pratt. 编辑3:这是@ chris-pratt中使用示例的代码。

@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
{
    <div>
        If applications do not appear in a separate tab or window, please click the button below.
    </div>
    <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" />
}

<script>
    window.onload = function () {
        document.forms['PrintApplications'].submit();
    }
</script>

The following simplistic HTML document functions exactly as you want: 以下简单的HTML文档完全可以根据需要运行:

<html>
<body>
    <form id="Test" action="http://google.com" target="_blank">
        <input type="hidden" name="q" value="test" />
    </form>
    <script>
        window.onload = function () {
            document.forms['Test'].submit();
        }
    </script>
</body>
</html>

I originally used document.onreadystatechange , which also worked, but noticed IE 11 apparently fires that event twice, resulting in two popups. 我最初使用document.onreadystatechange ,它也可以工作,但是注意到IE 11显然会触发该事件两次,从而导致两个弹出窗口。 Regardless. 而不管。 Perhaps by working backwards you can find your problem. 也许通过反向工作可以找到您的问题。

try thisway 尝试一下

mySubmitButton = document.getElementById("PerfSubmit");         
mySubmitButton.click();         

with below code the form will get submitted 使用以下代码,表单将被提交

$('form')[0].submit();

or 要么

mySubmitButton.trigger("click");

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

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