2016-04-10 19 views
1

Ich habe Formular Produkte suchen:So verwenden @ Html.BeginRouteForm für Suchformular

@using (Html.BeginForm("Search", "Results", FormMethod.Get, htmlAttributes: new { @class = "navbar-form navbar-left", role = "search" })) 
{ 
    <div class="form-group"> 
     <input type="text" name="txtSearch" class="form-control" placeholder="Products name..." /> 
     @Html.DropDownList("CategoryId", null, "Select category", new { @class = "form-control" }) 
     <select class="form-control" id="FromPrice" name="FromPrice"> 
      <option value="0">From price</option> 
      <option value="2000000">2,000,000d</option> 
      <option value="4000000">4,000,000d</option></select><select class="form-control" id="ToPrice" name="ToPrice"> 
      <option value="0">To price</option> 
      <option value="2000000">2,000,000d</option> 
      <option value="4000000">4,000,000d</option> 
      <option value="6000000">6,000,000d</option>      
     </select> 
     <input type="submit" class="btn btn-danger form-control" value="Search" /> 
    </div> 
} 

und eine Route in RouteConfig

routes.MapRoute(
    name: "SearchForm", 
    url: "ket-qua/{txtSearch}-{CategoryId}-{FromPrice}-{ToPrice}", 
    defaults: new { controller = "Results", action = "Search"} 
) 

Datei Wie kann ich die Route "Searchform" nennen, wenn Ich gebe das Suchformular oben mit allen Parametern ein.

+0

'@ Html.BeginRouteForm ("Searchform", FormMethod.Get)' aber die Werte werden als Query-String-Parameter hinzugefügt werden, nicht Route-Werte (Ihrem Browser nichts über Ihre Route Definitionen weiß), so erhalten Sie '/Ergebnisse/Suche? TxtSearch = abc & CategoryId = 2 & ..... ' –

Antwort

2

Ihr Routing-Code wird so aussehen.

 routes.MapRoute(
     name: "SearchForm", 
     url: "ket-qua/", 
     defaults: new { controller = "Results", action = "Search"} 
    ) 

In Ihrem Sichtcode wird es so sein.

@using (Html.BeginRouteForm("SearchForm", FormMethod.Get, htmlAttributes: new { @class = "navbar-form navbar-left", role = "search" })) 

{

Ihr Controller-Code wird wie auf diese Weise sein.

Wenn Sie Ihre URL so wollen. Sie müssen das mit Javascript tun. Und müssen Controller-Code ändern und Code anzeigen.

 <input type="button" class="btn btn-danger form-control" value="Search"  onclick="newDoc()" /> 
     function newDoc() { 
     //here you bind all your data 
     window.location.assign("http://----------") 
     } 
+0

Dank Anik Saha, wie Sie lehren, Website Arbeit und URL ist http: // localhost: 65012/Ket-qua? TxtSearch = ein & CategoryId = 1 & FromPrice = 2000000 & ToPrice = 4000000. Wie kann ich tun, wenn ich möchte, dass URL wie folgt http: // localhost: 65012/ket-qua/a-1-2000000-4000000 – giathienphu

+0

Erwarten Sie ein upvote von Ihnen. –