3

i want to add my customer javascript file to my cshtml

i am working with mvc4

i tried these two ways

@Scripts.Render("~/Scripts/Register.js")
<script type="text/javascript" src="~/Scripts/Register.js"></script>

nothing works.

i want to do this because i want to check for select change.

$(document).ready(function () {
    $('#selectRegisterType').on('change', function () {
        alert("ddddddd");
    });
});

also this is the code of the html

<select id="selectRegisterType">
    <option value="None">Select One</option>
    <option value="tenant">Tentant</option>
    <option value="Apartment Owner">Apartment Owner</option>
</select>

any help would be appreciated

0

2 Answers 2

2

Using MVC4 is really good and you should take advantage of the BundleCollection in your App_Start folder you'll see the BundleConfig.cs and there you can add your javascript and styles and others.

now what you need to write is

 public static void RegisterBundles(BundleCollection bundles)
  {
 bundles.Add(new ScriptBundle("~/Scrips/Register").Include(
             "~/Scripts/Register.js"));

   }

and in your footer tag inside your body tag

</footer>
@Scripts.Render("~/bundles/Register")
@RenderSection("scripts", required: false)
</body>

< /html>

You could otherwise use some script to link directly to your script url like

</footer>
       <script type="text/javascript">

                var e = document.createElement('script');
                e.src = '@Url.Content("~/Scripts/Register.js")';
                e.type = 'text/javascript';
                document.getElementsByTagName("head")[0].appendChild(e);

        </script> 
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

WHERE do you put your script? As this looks like JQuery code, do you load JQuery before?

I put your code in JSFiddle, looks like it's working just fine as is:

$(document).ready(function () {
    $('#selectRegisterType').on('change', function () {
        alert("ddddddd");
    });
})

http://jsfiddle.net/h2L6d/ (Duplicated your code here as I can't put a link to JSFiddle without some code)

It seems to me the only reason it would not be "working" is that you are not loading JQuery BEFORE calling your script. Usually, I load those js libraries below the footer of _Layout.cshtml:

       </footer>          
        @Scripts.Render("~/bundles/js") 
        <script src="~/Scripts/jquery.slidePanel.js"></script>            
        @RenderSection("scripts", required: false)        
    </body>

and scripts specific to a page in a script section, at the bottom of the cshtml view:

@section scripts {
  @Scripts.Render("~/bundles/jstree")  
}

This way I am sure my js libraries are always loaded before my local page scripts. The script which is not in a bundle has been added later on, and should be bundled too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.