views:

33

answers:

3

I have an MVC2 application with a form (like ya do). The user enters a query into the form (in the /Cars/Index view) and hits the "submit" button which posts to the Details action of CarsController - the Details view renders with results, and all is well and good.

The user can enter a URL (such as /Cars/Details/123-125) and they get the Details view with 123, 124 and 125 displayed, same as if you'd entered it on the form. Also well and good.

What I want to enable, if possible, is that when the user enters "123-125" or whatever in the form the URL also reflects the results - instead of "/Cars/Details" for a URL, which is what shows now, I want it to show "/Cars/Details/123-125".

For the life of me, I cannot figure out how this should be done.

Any assistance is appreciated.

A: 

Change your form to a GET instead of POST method (POST is the default), and it should just work.

<% using (Html.BeginForm("Action", "Controller", FormMethod.Get)) { %>
Andrew Barber
well, it was a step in the right direction - but the URL now looks like this, which is close but not quite: /Cars/Details?Query=123. I'm looking for /Cars/Details/123
ScottSEA
To get the URL to look like you want, you could add a new Route for that case. Are you familiar with adding routes? The path spec would be something like `/Cars/Details/{*Query}` and then set the Controller and Action defaults as appropriate.
Andrew Barber
I edited the above and added the asterisk in front of Query; you'd need that to be sure that the dash you suggested could be in the values does not cause routing to get confused and think you have two separate parameters.
Andrew Barber
A: 

It sounds like its not finding a matching route definition for /Cars/Details/123-125. I think this could be happening because of the way the routing engine works.

http://stackoverflow.com/questions/2568146/asp-net-mvc-route-contraints-with-id-slug-format

XSaint32
I don't believe that is the case - if you type /Cars/Details/123-125 into the address bar and hit enter *it works*. If you use the form it also works, but just has /Cars/Details/. If the route weren't there it wouldn't work at all.
ScottSEA
Hmmm... in your form, are you passing along the 123-125 in the RouteData?
XSaint32
@XSaint32 ; what ScottSEA said; his Controller Action works fine, but the default for Forms is that they will submit via POST, but he wants it to do via GET so the query info is in the query string. Routing does have some involvement here though, because of how he wants the resulting URL to appear. (see comments on my answer)
Andrew Barber
I have had a similar issue getting a form to submit values using GET and formatting the URL based on the routing rules.
XSaint32