I have a very simple form:
<% Html.BeginForm("Listing", "Home", Nothing, FormMethod.Get) %>
<%= Html.TextBox("id")%>
<%= Html.TextBox("id2")%>
<input type="submit" value="Submit" />
<% Html.EndForm()%>
This will generate a form with two input fields. If I enter 'test1' and 'test2' as text and submit the form, the end result will be:
http:// localhost/Home/Listing?id=test1&id2=test2
Is there a way to configure the MVC2 framework so that the开发者_JAVA技巧 end result can be like this:
http://localhost/Home/Listing/test1/test2
Of course, the alternatives are either writing a small Javascript to intercept the form submit, constructing the URL and redirecting the user on the client side, or to do a POST instead, construct the URL and then redirect the user on the server side.
Personally I'd follow the PRG pattern here.
The users fills in your form which then gets POST
ed back to the server, you perform any operations on the data you need and then redirect the user via a GET
to the appropriate place (redirect to action, route etc).
This has a couple of benefits for you. The first and most obvious is that the user can now refresh their destination page etc without the horrible "Refreshing this page will post bla bla back to the server" message.
The second benefit for you is that you can now redirect your user to the URL you prefer. In your case you'll build up the URL you're going to be redirecting too and then send them on their way.
i.e. http://localhost/Home/Listing/test1/test2
first of all, if you are using MVC forms, than use
<%= Html.DisplayFor(m => m.Id2) %>
instead of manually draw them.
And there is no possibility for a Html form to directly put the values in the URL. they either put it in the POST (Header variables) or GET (query strings). therefore, if you want the behaviour you have to do the Javascript by yourself.
and, btw, this only works, if your route accepts the value.
context.MapRoute("{controller}/{action}/{id}/{id2}")
精彩评论