Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 jquery scripting

I am used to MVC2... Where you simply add your links to scripts and your sources to the view and just call the jquery function and viola... You have working Jquery code. But I am unable to get my jqGrid tableToGrid function working. I am guessing cause I am not loading the jquery libraries in right at all or I am not running the scripts in the right place. Could you take a look at my code and see what could possible be wrong?

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ShowAllEncounters</title>
 <link href="@Url.Content("~/Content/jquery.jqGrid/ui.jqgrid.css")" rel="Stylesheet" type="text/css"/>
</head>

This would be the head... I previously had my scripts linked in there as well... But that didn't work... So I put outside of the tag... like so...

</html>
<script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"/>
<script type="text/javascript">
$(document).ready(function () {
...});

Am I missing something? How does one go about scripting jquery in an MVC4 site?

I can't even get a freaking alert to show up!!!!!!!!!!!!!

@model IEnumerable<FocusedReadMissionsRedux.Models.TemplateModel>
<!DOCTYPE html>

<html>
<head>
<script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"/>
<link href="@Url.Content("~/Content/jquery.jqGrid/ui.jqgrid.css")" rel="Stylesheet" type="text/css"/>
   <script type="text/javascript">
       $(document).ready(function () {
        alert("what is going on");
       });
   </script>
   <title>ShowAllEncounters</title>
</head>

UPDATE: It would seem as though I am having an issue with when the jquery libraries are being loaded. Which would definitely be a problem. I need a solid pattern for adding these scripts to a project?

like image 907
SoftwareSavant Avatar asked Jan 29 '26 12:01

SoftwareSavant


1 Answers

In the "new" MVC4 templates, jquery scripts are added at the "bottom" of the _layout.cshtml via Bundles.

Move the @Scripts.Render("~/bundles/jquery") within your block of the _layout.cshtml.

I'm not sure why the template puts this script include at the end of the html file...

like image 55
Patrice Calvé Avatar answered Jan 31 '26 15:01

Patrice Calvé



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!