最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ASP.net MVC javascript in partial view is not running - Stack Overflow

programmeradmin5浏览0评论

Maybe someone that can help me to understand what is the problem or error with this call to a partial view that has its own javascript code.

This is the main cshtml:

@model  Ads_Negocio.FormTest

@{
    ViewBag.Title = "Reportone";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div> <p>Parent view</p> </div>
<p>----------Partial view section-----------</p>
<div>
    <p>Partial view</p>
    @Html.Partial("Componente/_aTestView")
</div>

@section scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            alert("parent view renderign")
        });
    </script>
}

the partial view _aTestView.cshtml:

<button type="button" id="btnGraficNew">Run</button>

<script type="text/javascript">
    $(document).ready(function () {
        alert("partial view rendering");
        $("#btnGraficNew").click("click", function () {
            alert("Call from partial view");
        });
    });
</script>

Problems:

  1. After view is rendereng alert inside partial view is never call.
  2. When I press botton, it does not alert.

Maybe someone that can help me to understand what is the problem or error with this call to a partial view that has its own javascript code.

This is the main cshtml:

@model  Ads_Negocio.FormTest

@{
    ViewBag.Title = "Reportone";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div> <p>Parent view</p> </div>
<p>----------Partial view section-----------</p>
<div>
    <p>Partial view</p>
    @Html.Partial("Componente/_aTestView")
</div>

@section scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            alert("parent view renderign")
        });
    </script>
}

the partial view _aTestView.cshtml:

<button type="button" id="btnGraficNew">Run</button>

<script type="text/javascript">
    $(document).ready(function () {
        alert("partial view rendering");
        $("#btnGraficNew").click("click", function () {
            alert("Call from partial view");
        });
    });
</script>

Problems:

  1. After view is rendereng alert inside partial view is never call.
  2. When I press botton, it does not alert.
Share Improve this question asked Jul 31, 2020 at 23:02 Byron2017Byron2017 1,0034 gold badges13 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Partial View is loaded together with the parent view. Therefore, the javascript mand should be reside on the parent view rather than the partial view itself.

When you run your code you can see the below error in console window.

Uncaught ReferenceError: $ is not defined 

Solution - You should put the jquery reference in the head section of the _Layout.cshtml page. Once you do that your code will run as expected. If you are using default bundle then below code will work.

<head>
    ...
    @Scripts.Render("~/bundles/jquery")
</head>

or

<head>
    ...
    <script src="https://ajax.googleapis./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

window.addEventListener works on partial views.

Put your code inside addeventlistener method.

For example this code works in my partial views only with addEventListener.

<script defer>
 window.addEventListener("load", function () {
    $(document).on("change", "#ddlBirim", function () {
        window.location.href = "/@controller/@action/" + $(this).val();
    });
 });
</script>
发布评论

评论列表(0)

  1. 暂无评论