c# - How can I make form values appear as URL segments? -


i have store site uses mvc 4. i'm having trouble getting route right. posts search string. want display search string every time. in site layout have search box uses code:

_sitelayout.cshtml:

@using (html.beginform("search", "catalog")) {      <div class="input-group">           @html.textbox("searchstring", "", new dictionary<string, object> {{"placeholder", "enter search"}, {"class", "form-control input-search"}})           <span class="input-group-btn">                <button class="btn btn-default no-border-left" type="submit"><i class="fa fa-search"></i></button>           </span>       </div> } 

my routeconfig uses this:

routeconfig.cs:

routes.maproute(         "search",         "catalog/search/{searchstring}",         new {controller = "catalog", action = "search", searchstring = string.empty}); 

when enters search box on page, redirects them /catalog/search. search functionality works great, want show url: /catalog/search/i+searched+for+something

any assistance great. thanks!

this behaviour beyond control of mvc. if submit form via post, form values submitted in request body. if submit via get, values created on query string. controlled browser.

immediately, can see 1 possible solution, accept values on query string , redirect same action using query value route parameter instead (edit: thinking it, should still work if original request post):

@using (html.beginform("search", "catalog", formmethod.get)) 

and in action:

public actionresult search(string searchstring) {     if(!string.isnullorempty(request["searchstring"]))     {          return redirecttoaction("search", new { searchstring });     }      /* rest of processing here */ } 

this should check if value in query string and, if was, regenerate request same action. because mvc in control, should take route account , put parameter in path instead of query string.

this is, of course, http request every search should trivial.

alternatively, use jquery read form value, construct url , perform redirect.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -