AOY
AOY

Reputation: 355

Working with a separate js file in asp.net mvc application

I am creating an asp.net MVC application. I want to separate my js code from the razor code in view. So I've got a file with the path "~/Scripts/SearchProductsScript.js" _Layout.cshtml:

<head>
    ...
    <script src="@Url.Content("/Scripts/jquery-1.10.2.min.js")"></script>
    @if (IsSectionDefined("JavaScript"))
    {
        @RenderSection("JavaScript", required: false)
    }
  ...
</head>

_SomeView.cshtml:

@section JavaScript
{
    <script type="text/javascript" src="@Url.Content("~/Scripts/SearchProductsScript.js")"></script>
}

And SearchProductsScript.js:

var uri;

function test() {
    alert("TEST");
}

$(document).ready(function () {
    Doing something here
    $(':radio').change(function () {
        Doing something here
    });
});

I'm not sure how to properly use this separate js file. None of the functions is working and trying to call them from the _SomeView.cshtml doesn't work:

<script>
    $(document).ready(function () { test(); });
</script>

or

<input type="button" value="Search" onclick="test();" />

Upvotes: 1

Views: 1963

Answers (1)

AOY
AOY

Reputation: 355

So reason the js code was not called was that I was trying to set a section from the partial view which is not possible. RenderSection not working inside Partial View in ASP.NET MVC3

Upvotes: 1

Related Questions