Asp.net mvc 如何将javascript从局部视图添加到layout.cshtml页面

Asp.net mvc 如何将javascript从局部视图添加到layout.cshtml页面,asp.net-mvc,Asp.net Mvc,我试图通过调用 @RenderSection(“脚本”,必需:false),但失败。请查看我的partialview页面代码 @{ Layout = null; } <div id="modal-login" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable ui-resizable" title="Insert Student"&g

我试图通过调用 @RenderSection(“脚本”,必需:false),但失败。请查看我的partialview页面代码

@{
    Layout = null;
}

<div id="modal-login" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable ui-resizable" title="Insert Student">
    @using (Html.BeginForm("Login", "Home", FormMethod.Post, new { id = "form-login" }))
    {
        <div style="width:320px; height:320px; padding:0px 15px 15px 15px;  border:solid 1px silver">

            <div style="width:310px; height:30px; padding-bottom:0px; border:solid 0px red">
                <div style="width:320px; height:30px; float:left;">
                    <div id="loading" style="height:15px; width:120px">
                    </div>
                </div>
            </div>

            <div style="width:310px; height:30px; padding-bottom:35px; border:solid 0px red">
                <div style="width:320px; height:30px; float:left;">
                    <input type="text" class="textbox" id="tbEmailAddress" name="tbFirstName" size="50" placeholder="First Name" />
                </div>
            </div>

            <div style="width:310px; height:30px; padding-bottom:35px; border:solid 0px red">
                <div style="width:320px; height:30px; float:left;">
                    <input type="text" class="textbox" id="tbPassword" typeof="password" name="tbFirstName" size="50" placeholder="First Name" />
                </div>
            </div>

            <div style="width:320px; height:20px; padding-top:5px; padding-bottom:15px; border:solid 0px red; font-size:9px;">
                <div style="width:310px; height:30px; float:left;">
                    <input id="btnLogin" class="submit ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" value="Submit" style="border:1px solid gray; width:80px; height:25px ">
                </div>
                <div style="width:140px; height:30px; float:left;">

                </div>

            </div>

        </div>
    }
</div>

@section scripts
{

<script type="text/javascript">



        $(document).ready(function () {

            $("#modal-login").dialog({
                show: "slide",
                modal: true,
                autoOpen: false,
              });


            $("#btnLogin").click(function () {
                $("#modal-login").dialog("open");
                return false;
            });
        });

    </script>

}
@{
布局=空;
}
@使用(Html.BeginForm(“Login”,“Home”,FormMethod.Post,new{id=“form Login”}))
{
}
@节脚本
{
$(文档).ready(函数(){
$(“#模式登录”)。对话框({
放映:“幻灯片”,
莫代尔:是的,
自动打开:错误,
});
$(“#btnLogin”)。单击(函数(){
$(“模式登录”)。对话框(“打开”);
返回false;
});
});
}
下面是layout.chtml head部分,iam在最后一行代码中调用@RenderSection(“scripts”,required:false)

 <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - My ASP.NET Application</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/jquery")
        @*@Scripts.Render("~/bundles/jqueryui")*@


        <link href="~/Content/themes/base/jquery-ui-1.10.4.custom.css" rel="stylesheet" />
        @*<script src="~/Scripts/jquery-1.10.2.js"></script>*@
        <script src="~/Scripts/jquery-ui-1.10.4.custom.js"></script>

        @RenderSection("scripts", required: false)
</head>

@ViewBag.Title-我的ASP.NET应用程序
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/jquery”)
@*@Scripts.Render(“~/bundles/jqueryui”)*@
@**@
@RenderSection(“脚本”,必需:false)

请查看我的代码并建议我是否遗漏了任何内容?谢谢。

order@section脚本在partialView中不起作用,请删除它,然后脚本就可以工作了

但你为什么要把剧本放在头上

如果在头部调用JQuery,则不需要在头部使用部分视图中的脚本

但是,如果我理解您的代码,您可以在局部视图中创建一个登录表单,以便在布局中插入,以便在整个网站中使用


如果您直接在布局中的头部中编写脚本会更容易,但最好是使用所有自定义脚本创建一个脚本文件,将此脚本与其他脚本混合在一个捆绑包中,最后在头部调用此捆绑包,这样您的站点将更快。

按设计,分区在局部视图中不起作用。重复的
但失败的
-描述失败在此上下文中的含义。脚本是否不工作,是否没有呈现,是否存在异常?失败意味着!我试图将java脚本代码从部分视图添加到layout.cshtml的头部分,但我需要一个解决方案,因为我不想在版面页面中写上百行JavaScript。在布局页面中编写代码的原因是因为此JavaScript代码仅与此部分页面相关,所以我不想将代码拆分到多个位置以混淆自己。请推荐,谢谢。