0

I have a FormMethod.Get that takes two parameters from a mostly read only form. These two parameters are the only query parameters needed to display all the other values on the read only form so they are text boxes (most of the values are DisplayFor() or disabled). Normally I can navigate to my read only form by typing .../Home/Edit/S6/B043. However, because this is a form submission it shows a URL like this .../Home/Edit?Location=S6&Facility=B043. How do I get it to show my route of .../Home/Edit/S6/B043 when I submit the form to get the rest of the data? Or do I have to go back to my original plan of using javascript with window.location.href =. Thank you.

@using (@Html.BeginForm("Edit", "Home", routevalues, FormMethod.Get, new { id = "get-card" }))
{
  // code here
}
jmzagorski
  • 1,135
  • 18
  • 42
  • possible duplicate of [I'm not getting friendly url in a form with GET method](http://stackoverflow.com/questions/17583652/im-not-getting-friendly-url-in-a-form-with-get-method) – Erik Philips Oct 23 '14 at 21:44
  • My fault! I search the questions here before posting, but I guess not hard enough! – jmzagorski Oct 23 '14 at 21:49
  • The title to the other question is *terrible*, and it's very hard to search for technology specific terms as it stands. – Erik Philips Oct 23 '14 at 21:51
  • Don't get confuse even if you add a route to your RouteConfig file doesn't mean that your URL will look like that when you submit your form, it would only add another rout to get to your action, GET request it supposed to look like that with all the data you are sending back, if the information is sensitive you should use POST so you don't expose the data. – Goca Oct 23 '14 at 22:48

2 Answers2

1

Is there any special reason why you want to display your URL different? Whenever you submit(GET) the form the URL will have all the values and parameters you are sending back to your controller, if you are worried because some one will access the site using a URL like .../Home/Edit/S6/B043 then you can add that custom URL to your RoutConfig file like this.

routes.MapRoute("mycustomUrl", "Edit/{param1}/{param2}",
           new { controller = "controllerName", action = "Edit", 
                 param1 = UrlParameter.Optional,
                 param2 =UrlParameter.Optional }); 

After that you can call your view with .../Home/Edit/S6/B043 and will work. If you want to have a friendly URL you can use POST that way you will not see all the parameters on the URL but everything depends on what you want to accomplish.

Goca
  • 1,743
  • 2
  • 15
  • 36
  • No special reason I just wanted to make sure I did not miss any details in my research. I was just being picky when I saw that URL – jmzagorski Oct 24 '14 at 12:21
1

You could always hand write the form tags and use the UrlHelper to create the URL. It will then be created server side and show appropriately as you want it.

<form id="get-card" method="get" action="@Url.Action("Edit", "Home", routevalues)">
<!-- code here -->
</form>
Zack Z.
  • 238
  • 3
  • 10