Click here to Skip to main content
11,479,841 members (47,596 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
Good time! There was a problem displaying the route in the input URL browser. To the search page on the site. The search itself is working fine - the "key" is passed, the list of found displayed. Search method in the controller takes an argument of type string for which to search:


public ActionResult SearchAllByKey(string key)
 
            {
 
                //logic

                return View(<list_of_found>);
 
            } 


In Global.asax prescribed route:


routes.MapRoute(
 
                      "Search",
 
                      "Search/{key}",
 
                      new { controller = "controller_name", action = "SearchAllByKey", key = UrlParameter.Optional }
 
                  );


Form which sends the value of Edit to method from View:


<% using (Html.BeginForm("SearchAllByKey", "controller_name", FormMethod.Post, new { enctype = "multipart/form-data" }))
 
                               {%>
 
                            <%: Html.ValidationSummary(true) %>
 
                            <input type="text" id="keyValue" name="key" />
 
                            <input type="submit" value="Go!" />
 
                            <% } %>


When you click on "Go!". to a page of search results, but the URL (input line browser) shows:


http://localhost:PORT/Search


instead of:


http://localhost:PORT/Search/SOME_KEY


How to make sure that was visible "key" in the URL-e? Thanks in advance
Posted 7-Jan-13 13:39pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If the FormMethod.Post and before the method put [HttpGet], the URL looks:

localhost:PORT/Search?key=nature

but should be:

localhost:PORT/Search/nature
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution:
 
In View:
<pre lang="c#"><% using (Html.BeginForm("Search", "Home", FormMethod.Post))
{%>
    <% Html.ValidationSummary(true); %>
    <input type="text" id="key" name="key" value="" />
    <input type="submit" value="Go!" />
<% } %>

Add method to controller:

[HttpPost]
public ActionResult Search(FormCollection form)
{
    return RedirectToAction("SearchAllByKey",  new { key = form["key"] });
}

Thanks LeftyX.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 260
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 175
3 Richard MacCutchan 100
4 DamithSL 65
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100