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 January 11th, 2014, 11:17 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: Jan 2014
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 2 and I already broke it!?!

In following the tutorial through Chapter 2, everything was cool until I hit step 3 on pages 44-5. When I run the program and visit "/Store/Details/5", I get an error complaining about a null parameter for int id.
If I make int id nullable:
Code:
public string Details(int? id)
I don't see the error, but I id is not displayed in the output, either.
The output is displayed correctly if I visit "/Store/Detials?id=5".
"ASP.NET MVC's default routing convention is to treat the segment of a URL after the action method name as a parameter named ID." - has this behavior changed, do I need to change a setting in Visual Studio, or have I overlooked something?
Thanks in advance for any assistance.
  #2 (permalink)  
Old January 11th, 2014, 12:07 PM
Authorized User
Points: 175, Level: 3
Points: 175, Level: 3 Points: 175, Level: 3 Points: 175, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: St.Petersburg, Russia
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default

"ID" should not be nullable - just by nature; which item you are referring to if its ID is null? And if there are several of them with null IDs?

Do not make ID nullable; look for the error in some other place before it;

If "/Store/Details/5" gets you a null parameter, may be something is wrong with your routing;
The Following User Says Thank You to Oleg Kolpashchikov For This Useful Post:
lodieric (January 11th, 2014)
  #3 (permalink)  
Old January 11th, 2014, 12:44 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: Jan 2014
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Working- can't replicate error

Oleg,

I created a new project and recreated everything, and did not get the error. I reexamined the old project and could not see what was wrong. I deleted the Store controller and recreated it. (In retrospect, I should not have deleted it. Now I can't figure out what was wrong. Live and learn.)

Works correctly now. All I can think is that I had a typo somewhere, even though I really read through it closely.

Thanks for your help. Cheers.
  #4 (permalink)  
Old July 24th, 2014, 05:58 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: Jul 2014
Location: Edinburgh, UK
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same problem, and solution

I had the same problem. It works OK so long as your variable is id in upper or lower case. Its type doesn't matter, but you can't use any other variable name such as productID.
  #5 (permalink)  
Old July 25th, 2014, 03:42 AM
Authorized User
Points: 175, Level: 3
Points: 175, Level: 3 Points: 175, Level: 3 Points: 175, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: St.Petersburg, Russia
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default It's routing

You can use 'Id' without naming it because it's in your default routing:


routes.MapRoute(
name:
"Default",
url:
"{controller}/{action}/{id}",
defaults:
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);


Otherwise you will need to specify the name of the parameter in order for the binding to find it and put into your incoming variables:

Browser: /Store/Details/?ProductId=5

MVC: public ActionResult Details(int ProductId)
  #6 (permalink)  
Old November 18th, 2014, 05:09 AM
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: Nov 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I reexamined the old project and could not see what was wrong. I deleted the Store controller and recreated it. (In retrospect, I should not have deleted it. Now I can't figure out what was wrong. Live and learn.)
 


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
I broke it? Cliff2310 BOOK: Beginning Android Application Development 3 November 7th, 2011 02:34 AM
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM
CLIENT SIDE JAVASCRIPT BROKE THE PAGE jomaweb ASP.NET 1.0 and 1.1 Basics 0 September 12th, 2003 08:02 AM



All times are GMT -4. The time now is 06:13 PM.


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