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

Maybe someone that can help me to understand what is the problem or error with this call to a partial v

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744338802a4569277.html

相关推荐

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

    Maybe someone that can help me to understand what is the problem or error with this call to a partial v

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信