Skip to content

Routing in MVC2. Attributes are not displayed

Voprosec Kryzh

Asked Jan 7, 2013, 8:30 AM EST2 answers
Share
LinkedInX

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:


Code
    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

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    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
    0
  • 0
    Solution:

    In View:

    <% 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]

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

    Thanks LeftyX.
    0