views:

220

answers:

2

Hello,

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

+5  A: 

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".

Developer Art
+1 I changed the parameter name to "id" and it worked, thanks!
Roee Adler
+2  A: 

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

Alan Christensen