0

I have a form that represents a dropdown list of things. You select the desired thing and hit the okay button. It submits the form using a GET and the resulting URL is:

/Something/IdentificationInformation?jamesID=26

So I applied a routing rule:

        routes.MapRoute(
            name: null, // "Add James relationship"
            url: "James/IdentificationInformation/{jamesID}",
            defaults: new { 
                Controller = "James",
                action = "IdentificationInformation"
            }
        );

but this url route does not get applied?

The form being submitted looks like this:

        @using (Html.BeginForm("IdentificationInformation", "James", FormMethod.Get))
        {
            <div class="col-lg-9 col-md-9 col-sm-9 add-margin-top">
                    @Html.DropDownList("jamesID", new SelectList(Model, "JamesID", "Name"), new { id = "JamesDropdownList" })
            </div>
            <div class="col-lg-3 col-md-3 col-sm-3 add-margin-top">
                @Html.ContinueButton("Continue")
            </div>
        }
Charles
  • 50,943
  • 13
  • 104
  • 142
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • Anything I can add to this question to help get an answer? it's a bit odd. – Jimmyt1988 Feb 17 '14 at 14:12
  • Do you have any routes defined before this one that might be matched first? Also, in your Html.BeginForm() it looks like the controller is JamesController ... – Peter Feb 17 '14 at 14:20
  • No routes before, no. That was a typo, sorry and thanks for picking up on that. I've ended up having to do 2 controllers, one that takes a POST request from the form, then that redirects to another controller that returns the view. It's like the routing rules do not work for Form submits? – Jimmyt1988 Feb 17 '14 at 14:22

1 Answers1

0

The problem is that your route expects jamesID to be part of the url path but the form submits it as part of the querystring. If you change your route to something like this, it should match:

    routes.MapRoute(
        name: null, // "Add James relationship"
        url: "James/IdentificationInformation",
        defaults: new { 
            Controller = "James",
            action = "IdentificationInformation"
        }
    );

And, of course, this route probably isn't even necessary since it's equivalent to the default route of {controller}/{action}.

If you want to get fancy, you could define a custom QueryStringConstraint to ensure that jamesID is in the querystring as part of the route matching. Here's a SO question that shows how to do that:

Can my MVC2 app specify route constraints on Query String parameters?

Community
  • 1
  • 1
Peter
  • 12,541
  • 3
  • 34
  • 39
  • Thanks... It makes sense as to why... shame it's such a bother to implement :) . My current solution is 2 controller methods. One that takes a POST request from the form, then that redirects to another controller method that returns the view. 2 requests for one page just for a pretty URL... I guess the answer is it's a poor work around. I'll see if I can implement your idea. – Jimmyt1988 Feb 17 '14 at 15:44