10

I am barely starting out with my first project on the ASP.NET MVC project type and I am creating a Details page where instead of passing the templated (int id), I would like to pass a string instead. But when I am in debug mode, and enter this in the URL, "myString" is null. Why so? Do I have to change anything else somehwere else?

So if I go to the URL and enter this:

http://localhost:2345/Bank/EmployeeDetails/3d34xyz

public ActionResult EmployeeDetails(string myString) // myString is null
{
     return View();
}

6 Answers6

39

In you Global.asax.cs file, you will have the following route mapped by default:

routes.mapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

That means that an url like http://localhost:2345/Bank/EmployeeDetails/3d34xyz will go to the Bank controller, the EmployeeDetails action and pass the value 3d34xyz into a parameter named id. It is perfectly alright to pass a string, but in order to make it work you have two options:

1) Rename the variable to id in your action method.

public ActionResult EmployeeDetails(string id) { ... }

2) Add another route that matches whatever name you want for your string. Make sure to make it more specific than the default route, and to place it before the default route in the Global.asax.cs file.

routes.mapRoute(
    "BankEmployeeDetails"
    "Bank/EmployeeDetails/{myString}"
    new { controller = "Bank", action = "EmployeeDetails", myString = UrlParameter.Optional });

This will pass a default value of null to myString if no value is passed in the url, but with the url you specified you will pass the value 3d34xyz.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • 1
    Just fyi, I did find a small error in my post: if you choose option 2 and add a new route, it should be more _specific_ than the default route, not more _general_ as I originally stated. This is because the MVC Framework will go through the routes in order, and grab the first one that matches - so you want to make sure that only the url:s that are relevant match the new route. – Tomas Aschan May 11 '09 at 19:41
  • just an fyi for anyone looking `id = null` might be `id = UrlParameter.Optional` depending on your version of .NET – petrosmm Oct 20 '16 at 17:12
  • 1
    @MaximusPeters: Thanks - probably no-one getting here today will use a version where `null` is the right way to do it :) – Tomas Aschan Oct 20 '16 at 17:16
  • Thanks so much @Tomas Lycken - this helped me with my issue as well. – blacksaibot Mar 01 '17 at 11:46
5

Rename myString to id if you are using the default route table.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
5

Assuming you haven't modified the default routes (In your Global.asax.cs):

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

The method is expecting it to be named "id".

David P
  • 3,604
  • 3
  • 37
  • 54
4

Change the name of myString to id.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0

I had the same problem, you just need to provide the id in the page where you the hyperlink for Details(in the Bank page here)

 @Html.ActionLink("Details", "Details", new { id=""}) 

This solved my problem hope it helps you.

This for MVC 5.

Kushal
  • 17
  • 7
0

And when you have tryed all of them and still it comes back null then give a full restart for Visual Studio. That's what eventually helped for me.