Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Professional ASP.NET MVC 4
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 25th, 2012, 05:08 AM
Registered User
 
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
 
Old October 31st, 2012, 03:29 PM
Registered User
 
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)
 
Old October 31st, 2012, 11:19 PM
Registered User
 
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
 
Old May 9th, 2013, 09:57 PM
Authorized User
 
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?
 
Old June 4th, 2013, 03:10 PM
Registered User
 
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.





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





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