簡體   English   中英

未捕獲的TypeError:無法調用null的方法“提交”

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

這是錯誤的圖片。 在對項目進行一些更改后,將出現此錯誤。 當您按下按鈕時,它應該轉到用於記錄網站用戶的控制器。 我重新制作了布局以進行引導並更改/刪除了屬於該站點的許多CSS。 這可能是相關的,但不確定如何。 這是按鈕的代碼:

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

有誰知道為什么會這樣?

我認為您的問題就在這條線上。

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

檢查您的頁面源,我想您的操作在表單上看起來像這樣。

action="Account/LogOff?id=logoutForm"

解:

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

那應該為您解決:)

更新:

您可以切換代碼以使其工作方式有所不同,但仍然可以使用。

選項1:

將此粘貼到布局文件的末尾。

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

選項2:

將標簽更改為

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

選項3:

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

我個人喜歡選項2,因為您只是使用應有的提交按鈕,而不是JavaScript。

更新#2:

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

應產生:

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

注意操作方法屬性。 此外,請確保從代碼中刪除文本標簽,因為它們旨在強制Razor語法接受它們之間的所有內容作為文本,而不是Razor語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM