简体   繁体   English

未捕获的TypeError:无法调用null的方法“提交”

[英]Uncaught TypeError: Cannot call method 'submit' of null

Here is a picture of the error. 这是错误的图片。 This error appeard after I made some changed to my project. 在对项目进行一些更改后,将出现此错误。 When you press the button it's supposed to go to a controller for logging a user of the website. 当您按下按钮时,它应该转到用于记录网站用户的控制器。 I remade my layout to bootstrap and changed/deleted alot of the CSS belonging to the site. 我重新制作了布局以进行引导并更改/删除了属于该站点的许多CSS。 This might be related but not sure how. 这可能是相关的,但不确定如何。 Here is the code for the button: 这是按钮的代码:

@if (Request.IsAuthenticated) {
    <text>
        Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
        @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
            @Html.AntiForgeryToken()
            <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
        }
    </text>
} else {
    <ul>
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

Does anyone know why this might be occurring? 有谁知道为什么会这样?

I would assume your problem is on this line. 我认为您的问题就在这条线上。

@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {

Check your pages source, and I would be that your action looks something like this on your form. 检查您的页面源,我想您的操作在表单上看起来像这样。

action="Account/LogOff?id=logoutForm"

Solution: 解:

@using (Html.BeginForm("LogOff", "Account", null, FormMethod.Post, new { id = "logoutForm" })) {

That should fix it for you :) 那应该为您解决:)

Update: 更新:

You can switch the code around to make it work differently, but still work. 您可以切换代码以使其工作方式有所不同,但仍然可以使用。

Option 1: 选项1:

paste this at the end of your Layout file. 将此粘贴到布局文件的末尾。

<script>$(function() {
    $('#logoutForm a').on('click', function () {
          $('#logoutForm').submit();
     });
</script>

Option 2: 选项2:

Change the tag to a 将标签更改为

<button type="submit">Log Off</button> <!-- no JavaScript -->

Option 3: 选项3:

<!-- I think this will work -->
<a onclick="document.getElementById('logoutForm').submit()">Log Off</a>

I personally like option 2, because you are just using a submit button like you are supposed to, instead of JavaScript. 我个人喜欢选项2,因为您只是使用应有的提交按钮,而不是JavaScript。

Update #2: 更新#2:

@using(Html.BeginForm("LogOff", "Account", null, FormMethod.Post, new { id = "logoutForm" }) {
    @Html.AntiForgeryToken()
    <button type="submit">Log Out</button>
}

Should produce: 应产生:

<form action="/Account/LogOff" method="POST" id="logoutForm">
   <input type="hidden" name="__RequestVerification" value="***" />
   <button type="submit">Log Out </button>
</form>

Take note of the action and the method attributes. 注意操作方法属性。 Additionally make sure to remove the text tags from your code as they are meant to force Razor syntax to accept whatever is between them as text and not Razor syntax. 此外,请确保从代码中删除文本标签,因为它们旨在强制Razor语法接受它们之间的所有内容作为文本,而不是Razor语法。

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

相关问题 未捕获的TypeError:无法调用null的方法“ appendChild”? - Uncaught TypeError: Cannot call method 'appendChild' of null? 未被捕获的TypeError:无法调用null的方法&#39;getElementsByTagName&#39; - Uncaught TypeError: Cannot call method 'getElementsByTagName' of null 未捕获的TypeError:无法调用null的方法&#39;replaceChild&#39; - Uncaught TypeError: Cannot call method 'replaceChild' of null 未捕获的TypeError:无法调用null的方法“ menuAim” - Uncaught TypeError: Cannot call method 'menuAim' of null 未捕获的TypeError:无法调用null的方法'replace' - Uncaught TypeError: Cannot call method 'replace' of null 未捕获的TypeError:无法调用null的方法“ show” - Uncaught TypeError: Cannot call method 'show' of null 未捕获的TypeError:无法调用null的方法“ get” - Uncaught TypeError: Cannot call method 'get' of null InvalidStateError:DOM异常11和未捕获的TypeError:无法调用null的方法&#39;submit&#39; - InvalidStateError: DOM Exception 11 and Uncaught TypeError: Cannot call method 'submit' of null XML,JavaScript-未捕获的TypeError:无法调用方法&#39;getElementsByTagName&#39;为null - XML, Javascript - Uncaught TypeError: Cannot call method 'getElementsByTagName' of null 如何修复“Uncaught TypeError:无法调用方法&#39;appendChild&#39;的null” - How to fix “Uncaught TypeError: Cannot call method 'appendChild' of null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM