cannot add javascript file to cshtml
13,006
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>
Related videos on Youtube
Author by
Marco Dinatsoli
Updated on June 04, 2022Comments
-
Marco Dinatsoli almost 2 years
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