20

I have the following code in my controller called UserController:

public ActionResult Details(string name)
{
    MyModelDataContext db = new MyModelDataContext();
    Product user = db.Products.Single(t => t.Name == name);
    return View(user);
}

I expect that when I browse directly to http://localhost:port/User/Details/SomeName, I will reach this function with the "name" parameter containing "SomeName". I do get to this function, but "name" is null. I didn't change any of the default settings of the project.

What am I doing wrong?

Thanks

2
  • Post please your route definitions from Global.asax. Commented Aug 13, 2009 at 13:19
  • @New in town: I did not change it, it's the default Commented Aug 13, 2009 at 13:20

2 Answers 2

24

If you haven't changed your route definitions in Global.asax.cs then I suppose it still uses the default "id" name for the parameter. Either change it there to "name" or rename your action parameter to "id".

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I changed the parameter name to "id" and it worked, thanks!
4

Have you defined a route in your global.asax that includes the 'name' parameter?

routes.MapRoute(  
"Default",                                       // Route name  
"{controller}/{action}/{name}",                    // URL w/ params  
new { controller="Home", action="Index",name="" }  // Param defaults  
);  

See the example in the NerdDinner tutorial: http://nerddinnerbook.s3.amazonaws.com/Part4.htm

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.