tags:

views:

48

answers:

1

I have a action on my controller (controller name is 'makemagic') called 'dosomething' that takes a nullable int and then returns the view 'dosomething.aspx'. At least this is what I am trying to do. Seems no matter I get routed to the Default() view.

public ActionResult dosomething(int? id)
{       
  var model = // business logic here to fetch model from DB
  return View("dosomething", model);
}

There is a /Views/makemagic/dosomething.aspx file that has the Inherits System.Web.Mvc.ViewPage

Do I need to do something to my routes? I have just the 'stock' default routes in my global.aspx.cs file;

public static void RegisterRoutes(RouteCollection routes)
{
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

}

I am calling the action via a href like this in another page;

<a href="/makemagic/dosomething/25">Click Me!</a>

Seriously driving me nutso. Any suggestions on how to troubleshoot this? I attempted to debug break on my route definitions and seems a break there doesn't happen as one would expect.

+2  A: 

Change it so the parameter isn't nullable so it will match the default route, or change the name to something other than id and supply it as a query parameter. An example of the latter would be:

public ActionResult dosomething(int? foo)
{       
  var model = // business logic here to fetch model from DB
  return View("dosomething", model);
}

<a href="/makemagic/dosomething?foo=25">Click me</a>

The it will work with the default routing implementation. Alternatively, you could do something that would distinguish it from the default route and then you would be able to have a route for it and not have to use query parameters.

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

<a href="/makemagic/dosomething/foo/25">Click Me!</a>
tvanfosson
Thanks for your reply. I guess I am just missing something:Action like this;ActionResult dosomething(int foo) {}Tried this, failed;<a href="/makemagic/dosomething/15">Click</a>Tried this, failed;<a href="/makemagic/dosomething?foo=15">Click</a>Keeps sending me to the /makemagic/details action !?!?!Seems odd if I have to setup a new route, surely this is a common scenario? Yes?
CmdrTallen
Even tried <%= Html.ActionLink("makemagic", "dosomething", new { foo = 25 })%> and that also failed. Grrrr.
CmdrTallen
If I copy the URL and paste it into a new window it works, but it seems from within the page it gets 'lost'. This works from the browser when I paste it;http://localhost:port/makemagic/dosomething/foo/25But when as a href it seems to go to;http://localhost:port/home/defaultIdeas?
CmdrTallen
doh! I figured it out. I had a jQuery Colorbox() method monkeying with the URL. Gotta go smack myself now...
CmdrTallen