Wrox Programmer Forums
|
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 January 11th, 2014, 11:17 AM
Registered User
 
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.
 
Old January 11th, 2014, 12:07 PM
Authorized User
 
Join Date: Jun 2013
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)
 
Old January 11th, 2014, 12:44 PM
Registered User
 
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.
 
Old July 24th, 2014, 05:58 PM
Registered User
 
Join Date: Jul 2014
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.
 
Old July 25th, 2014, 03:42 AM
Authorized User
 
Join Date: Jun 2013
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)
 
Old November 18th, 2014, 05:09 AM
Registered User
 
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.)





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





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