Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Professional ASP.NET MVC 4
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 4
This is the forum to discuss the Wrox book Professional ASP.NET MVC 4 by Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen; ISBN: 978-1-118-34846-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 25th, 2012, 05:08 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Sample Code Error - Chapter 3 Specifying Views

While running the sample code for this chapter after installing Nuget Package Wrox.ProMvc4.Views.SpecifyingViews, I get an error while navigation to Sample/PartialViewDemo.

Unhandled exception at line 4, column 5 in http://localhost:9725/Sample/partialviewdemo

The value of the property $ is null or undefined.

Can you help me understand the Javascript which raised this error

Balachander
  #2 (permalink)  
Old October 31st, 2012, 03:29 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

The error is because partialViewDemo is writing script into the body and executing it before jQuery is loaded.

Views/_viewStart.cshtml defines the Layout as discussed in the book. In Views/Shared/_Layout.cshtml
Code:
@Scripts.Render("~/bundles/jquery")
is at the bottom of the page. Move it to to the <head> section of the document so jQuery is available before the script (in the body) executes.

There are other "more correct" ways of accomplishing same, but this will get you through the sample.
The Following User Says Thank You to szozz For This Useful Post:
mlcmds (October 31st, 2012)
  #3 (permalink)  
Old October 31st, 2012, 11:19 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by szozz View Post
The error is because partialViewDemo is writing script into the body and executing it before jQuery is loaded.

Views/_viewStart.cshtml defines the Layout as discussed in the book. In Views/Shared/_Layout.cshtml
Code:
@Scripts.Render("~/bundles/jquery")
is at the bottom of the page. Move it to to the <head> section of the document so jQuery is available before the script (in the body) executes.

There are other "more correct" ways of accomplishing same, but this will get you through the sample.
Thanks szozz... It solved the problem.

Cheers

Balachander
  #4 (permalink)  
Old May 9th, 2013, 09:57 PM
Authorized User
Points: 148, Level: 2
Points: 148, Level: 2 Points: 148, Level: 2 Points: 148, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 26
Thanks: 1
Thanked 4 Times in 3 Posts
Default Explain how partialviewdemo action works

Hello,
I had to do the same thing to fix the partialviewdemo. However, that and the book do not explain how the partialviewdemo action works.

The book just discusses this part

Code:
        
public ActionResult Message() {
            ViewBag.Message = "This is a partial view.";
            return PartialView();
        }
It does not explain how that works with
Code:
        
public ActionResult PartialViewDemo() {
            return View();
        }
How does the partialViewDemo method know to use the actionResult Message? Couldn't it just as easily assume to use one of the others like Index2?

It really seems to me that partialViewDemo should return a blank view (with just the stuff from _Layout.cshtml) since there is no code in the method defining the ViewBag.Message or anything. Can someone explain this?
  #5 (permalink)  
Old June 4th, 2013, 03:10 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Notso View Post
Hello,
I had to do the same thing to fix the partialviewdemo. However, that and the book do not explain how the partialviewdemo action works.

The book just discusses this part

Code:
        
public ActionResult Message() {
            ViewBag.Message = "This is a partial view.";
            return PartialView();
        }
It does not explain how that works with
Code:
        
public ActionResult PartialViewDemo() {
            return View();
        }
How does the partialViewDemo method know to use the actionResult Message? Couldn't it just as easily assume to use one of the others like Index2?

It really seems to me that partialViewDemo should return a blank view (with just the stuff from _Layout.cshtml) since there is no code in the method defining the ViewBag.Message or anything. Can someone explain this?
You have to look into the PartialViewDemo.cshtml

Code:
<div id="result"></div>

<script type="text/javascript">
    $(function () {
        $('#result').load('/sample/message');
    });
</script>
As you see the HTML of "/sample/message" will be loaded into the result div.

So, at first the controller executes the "PartialViewDemo()"-Action, which returns the HTML-Code shown above. But during loading the page, the JavaScript requests the HTML of "/sample/message". Now the controller executes "Message()", which returns the partial view and the JavaScript loads this into the div.

I hope this is explained clearly :-)

Cheers.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing to follow code in Chapter 3 (Views) gjok BOOK: Professional ASP.NET MVC 3 1 February 21st, 2013 08:51 AM
Error Trying to run sample code from chapter 9 mzaretsky99 BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB 0 February 17th, 2011 10:58 PM
Error in Chapter 11 Sample Code josevi BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 1 February 1st, 2008 01:43 PM



All times are GMT -4. The time now is 10:55 PM.


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