Razor 视图 MVC 5 JavaScript - 解析 document.getElementById

hnc*_*ncl 1 javascript asp.net-mvc razor

将我的 MVC3 (aspx) 项目迁移到 MVC5 (razor),我使用:

                <% if ((bool)this.ViewData["ReadOnly"] != true)
                   { %>
                    document.getElementById("NextAction").value = nextAction;
                    document.getElementById("VisitForm").submit();
                <% } else { %>
                    window.location = nextAction;
                <% } %>
Run Code Online (Sandbox Code Playgroud)

我将其修改为:

                     @if ((bool)this.ViewData["ReadOnly"] != true)
                     {
                       document.getElementById("NextAction").value = nextAction;
                       document.getElementById("VisitForm").submit();
                     } else {  
                        window.location = nextAction;
                     }  
Run Code Online (Sandbox Code Playgroud)

无法解析文档或窗口?

Ste*_*ick 5

您缺少脚本标签。像这样:

@if ((bool)this.ViewData["ReadOnly"] != true)
{
    <script>
        document.getElementById("NextAction").value = nextAction;
        document.getElementById("VisitForm").submit();
    </script>

}
else
{
    <script>
        window.location = nextAction;
    </script>

}
Run Code Online (Sandbox Code Playgroud)