Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC: Form Input to pretty URLs
    text
    copied!<p>I have a URL:</p> <p>Shipment/Search/{searchType}/{searchValue}</p> <p>and a controller action:</p> <pre><code>// ShipmentSearchType is an enum ... PartNumber, CustomerOrder, etc... ActionResult Search(ShipmentSearchType searchType, string searchValue) </code></pre> <p>So this means I can type in pretty urls like:</p> <p>Shipment/Search/PartNumber/Widget-01</p> <p>And get a list of all the shipments with that part number.</p> <p>Now I'm doing the busy work of the application and got to the point where I am making a search form that asks for Part Number and it will post back to Search. So basically I want:</p> <p>Shipment/Search/PartNumber/{user-input-from-textbox}</p> <p>Unfortunately I can't have a form get to the above url -- it has to be generated server side. So instead I'll have the form post back to Shipment/Search/PartNumber with {user-input} as a post request value.</p> <p>So I end up with:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] ActionResult Search(ShipmentSearchType searchType, string searchValue, bool? post) { return RedirectToAction("Search", new { searchType = searchType, searchValue = searchValue}); } </code></pre> <p>2 things:</p> <p>1) Is there a way I can get around having the post method of Search without using javascript on the client side?</p> <p>2) The bool? post value is there just so they have different signatures. That is obviously ugly. Is there a smarter way to do this?</p> <p>Thanks!</p> <p>edit:</p> <p>"Unfortunately I don't think I can do that from a form (without javascript at least)." &amp; "Is there a way I can get around having the post without using javascript?"</p> <p>This was a bit ambiguous. What I mean is that I don't think I can have a form generate the url /Shipment/Search/PartNumber/{value-from-textbox} and have it to a form method get. I think this would be simple to do in javascript (override the submit action to build the url dynamically) but I haven't done that. I didn't mean that javascript is necessary to do a post.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload