views:

191

answers:

2

I'm not doing any fancy route patterns yet, just the basic controller, action, id style.

However, my actions never seem to be passed the id. When I stick a breakpoint in any one of my actions, the value of the id parameter is null. What gives?

Global.asax.cs:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
    }

    protected void Application_AuthenticateRequest()
    {
        if (User != null)
            Membership.GetUser(true);
    }
}

Index() action on TenantsController.cs:

/// <summary>
    /// Builds the Index view for Tenants
    /// </summary>
    /// <param name="tenantId">The id of a Tenant</param>
    /// <returns>An ActionResult representing the Index view for Tenants</returns>
    public ActionResult Index(int? tenantId)
    {
        //a single tenant instance, requested by id
        //always returns a Tenant, even if its just a blank one
        Tenant tenant = _TenantsRepository.GetTenant(tenantId);

        //returns a list of TenantSummary
        //gets every Tenant in the repository
        List<TenantSummary> tenants = _TenantsRepository.TenantSummaries.ToList();

        //bilds the ViewData to be returned with the View
        CombinedTenantViewModel viewData = new CombinedTenantViewModel(tenant, tenants);

        //return index View with ViewData
        return View(viewData);
    }

The value of the tenantId parameter is aways null!!! Argh! The silly part is that when I use Phil Haack's Route Debugger, I can clearly see that the debugger sees the id. What the crap?!

+4  A: 

I think you're controller method's parameter name needs to match the name that is in the route string. So if this is in your global.asax:

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

Your controller method should look like this (notice the parameter name is 'id', not 'tenantId'):

public ActionResult Index(int? id)
MrDustpan
+5  A: 

Change the method to Index( int? id ) rather than Index( int? tenantId ) and it will be filled through routing.

In your routes, you've declared the variable to be named "id", but you're then trying to access it using "tenantId". tenantId will be filled if, for example, you access a page and add the query string ?tenantId=whatever.

ASP.NET MVC makes heavy use of reflection, so the names you give your methods and parameters matters in cases like these.

Stephen Jennings
Correct! Someone pointed this out to me in the MS MVC forums as well. It was a forehead slapping moment for sure. I just changed the action's parameter name to match the route's "id". Thanks!
NovaJoe