views:

422

answers:

3

I'm trying to learn MVC and having a rough start.

routes.MapRoute(
       "Test",
       "Test/{stringInput}",
       new { controller = "Test", action = "TestMethod", stringInput = "" }
);

doesn't pass stringInput to the method TestMethod in the controller. It comes over null.

Not sure what I'm missing, it seems very simple and straightforward. This route was placed above the default.

A: 

Are you sure that route is the one being used? Try moving it to the top of your list of routes to make sure.

Alan Jackson
Yes, I've followed it in the debugger.
James
+1  A: 

Make sure your controller is setup properly. It should be in folder

Controllers/TestController.cs

and inside the controller should be

public ActionResult TestMethod( string stringInput )
{
    return View();
}

It uses conventions, so the naming you setup in your route needs to match the files, methods, and parameters of the controller.

The url to get to this should be

/Test/TestMethod/MyStringInput

and "MyStringInput" would be the value of the variable stringInput.

Josh Close
+3  A: 

Override the Execute method of your controller and then put a breakpoint in it so you can see the request context. One of the properties is the key/value pair being passed in. Make sure the key for stringInput has the correct value.

Damien
Thank you, this helped me immensely in figuring out that I did not use the same parameter name in the signature of my controller method which is the issue...
James