p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET MVC 5 (http://p2p.wrox.com/forumdisplay.php?f=770)
-   -   Ch 8 AJAX (http://p2p.wrox.com/showthread.php?t=93570)

AlanWheeler September 16th, 2014 06:24 PM

Ch 8 AJAX
 
Hello Jon,

While I understand your book isn't intended to be an in depth tutorial on AJAX (I will ultimately purchase an AJAX book), I don't seem to be able to follow some of the introductory level material.

I created a 'Sandbox' View page as follows and it works as expected, it hides the h2 element when I mouse over it.

Code:

@Code
  ViewData("Title") = "Sandbox"
End Code

  <script src="~/scripts/jquery-1.10.2.js"></script>
  <script>
    $(document).ready(function () {
      $("h2").hover(function () {
        $ (this).hide();
      });
    });
  </script>
  <h2>Sandbox</h2>

  <p id="p1">This is a paragraph.</p>

(I'm reading Kindle, so no page numbers)

Just after Figure 8.1 the book explained the <script src=... concept as shown above. But just after that it states the following can be used instead for a variety of benefits:

Code:

@Scripts.Render("~/bundles/jquery")
The text specifically suggests to use this approach in the _Layout.cshtml file. But in that file the line above is near the bottom of the page. When I remove my <script src="~/scripts/jquery-1.10.2.js"></script> from the file in order to make use of the 'Render' line shown above, I get an "Unhandled exception error" because " '$' " is undefined. The following does, however, work.

Code:

@Code
  ViewData("Title") = "Sandbox"
End Code

  @Scripts.Render("~/bundles/jquery")

  <script>
    $(document).ready(function () {
      $("h2").hover(function () {
        $ (this).hide();
      });
    });
  </script>
  <h2>Sandbox</h2>

  <p id="p1">This is a paragraph.</p>

Being new to AJAX, I'm sure this is a basic question, but the book doesn't really address what must be some assumed knowledge about making this work by relying on the _Layout.cshtml code.

Can you shed some light?

Thanks.

Best Regards,
Alan

rfalanga November 18th, 2014 04:17 PM

Re: Ch 8 AJAX
 
Hi Alan,

I don't know the answer to your question, but I'm wondering if you might be missing a reference to the jQuery library in your code?

JonGalloway January 11th, 2015 01:27 AM

Alan, as Rod said you're definitely missing the jQuery library. It's possible that you have a different version of jQuery in your scripts folder (other than 1.10.2). Do you see a jQuery file in your /scripts folder?

AlanWheeler January 11th, 2015 02:19 AM

Thanks for the response, Jon. I'll have to get back to this setup as it was an older post, but when I am able to check this out I'll come back to this forum.

Best Regards,
Alan


All times are GMT -4. The time now is 01:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.