Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think JavaScript is your only option here. The form tag can only submit to one URL, so if you want to avoid the RedirectProduct route (which I would agree, is awkward), you'll have to do javascript. Something like this (using jQuery) might do the trick:</p> <pre><code>&lt;script type="text/javascript"&gt; function doredirect() { window.location="http://www.mysite.com/Products/" + $('#sku').val } &lt;/script&gt; &lt;% using (Html.BeginForm("ViewProduct", "Products")) { %&gt; &lt;%= Html.DropDownList("sku", ViewData.Model.ProductsList, new { onchange="doredirect" })%&gt; &lt;%-- Won't need this anymore: 'Html.SubmitButton()' --%&gt; &lt;% } %&gt; </code></pre> <p>(DISCLAIMER: I haven't fully tested that out, but if you run in to problems, I'll try to help in the comments for this answer)</p> <p>Unfortunately, you lose the benefit of routing, since the URLs are generated on the client. If you change your routes, you'll have to manually update this code. One possible solution to that is to trick the routing system a little and generate a "template" URL on the server, to pass to your client code. Like this:</p> <pre><code>&lt;script type="text/javascript"&gt; var urlTemplate = &lt;%= Url.Action("ViewProduct", "Products", new { sku="__sku__" }) %&gt; function doredirect() { window.location= urlTemplate.replace(/__sku__/,$('#sku').val) } &lt;/script&gt; </code></pre> <p>(Same Disclaimer as above :P)</p> <p>The server-side code should generate a URL like: <code>http://mysite.com/Products/__sku__</code>, then the javascript replaces the <code>__sku__</code> token with the actual SKU.</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