Category: .NET

C# JQuery bundle not loading?

I was having issues with my JQuery script not working in C# Visual Studio 2013

The _Layout.cshtml had the required JQuery bundle in it @Scripts.Render(“~/bundles/jquery”)  & BundleConfig.cs had  bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include(“~/Scripts/jquery-{version}.js”));

So way the heck doesn’t my script work

Answer because I didn’t surround the script call line with:

@section scripts{
  <script src="~/Scripts/ifDeviceAndroidRemovePrintButton.js"></script>
}

All because I had this at the top of the view & it was being called before the JQuery bundle loaded, therefore it did not know about JQuery and where to find it.

Moving the call script line to the last loaded item of the view and surrounding it with @Section scripts did the trick.

Simple JQuery Onclick within Visual Studio

I wanted to perform what I thought would be a simple task..

Select a Radio button and make this perform its task without submitting the form with an extra click on a button.

After much searching and trial and error I found that this task can be done with a simple extension to the radio button:

onClick = "$(this).closest('form').submit();"

Here it is in context:

@Html.Label("Student")

@Html.RadioButtonFor(model => model.RequiredFilter, "Student", new { @class = "radioAlignLeft", onClick = "$(this).closest('form').submit();" })

Hope it helps, if I had found this blog post it would have helped me!