Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot add javascript file to cshtml

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

like image 521
Marco Dinatsoli Avatar asked Sep 09 '26 17:09

Marco Dinatsoli


1 Answers

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>
like image 168
photowalker Avatar answered Sep 11 '26 15:09

photowalker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!