Add Javascript to MVC Partial View

Stacey Fontenot
Stacey Fontenot used Ask the Experts™
on
I have an MVC C# Web Application and need to add some javascript to a partial view. I cannot seem to get the partial view to recognize the javascript. Every time I run the partial view in google dev, I get the error "cannot find the function". How can I include javascript in a partial view?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Developer
Distinguished Expert 2018
Commented:
Could you please show us what have you tried so far, have you tried :

@section Scripts {
    <script type="text/javascript">
	    $(document).ready(function () {
	        //MY JS code here
	    });
    </script>
}

Open in new window


OR just :

@{
	<script type="text/javascript">
	    $(function () {
	        //MY JS code here
	    });
    </script>
}

Open in new window


Or also :

@Scripts.Render("~/bundles/my-script")

Open in new window

Author

Commented:
@{
      <script type="text/javascript">
          $(function () {
              //MY JS code here
          });
    </script>
}

Worked. Thanks.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
You're welcome, Glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial