2016-11-01 5 views
0

我々は、GETメソッドでフォームを送信するとき、それは以下のようなクエリ文字列としてパラメータを渡すには: http://localhost:2564/Blog?SearchManufacturer=landMVC形式で特定の形式で表示し、クエリ文字列

しかし、私は以下のようなクエリ文字列を表示したい:

http://localhost:2564/Blog/SearchManufacturer/land

私は以下のコードを試しました。それでもクエリ文字列を渡します。

@using (Html.BeginForm("Index", "Blog", new { CurrentFilter = Model.SearchManufacturer }, FormMethod.Get)) 
    { 
    <div class="form-group col-lg-4 col-md-6 col-sm-6 col-lg-12"> 
     <label>Search Manufacturer</label> 
@Html.TextBoxFor(x => x.SearchManufacturer, new { @class = "form-control" }) 
</div>             
<div class="form-group col-lg-4 col-md-6 col-sm-6 col-lg-12"> 
<input type="submit" value="Search" class="submit" /> 
</div> 
} 

また、route.configでは、次のようにルーティングのさまざまな組み合わせを使用しました。

routes.MapRoute("Blog", "Blog/SearchManufacturer/{SearchManufacturer}", defaults: new { controller = "Blog", action = "Index" });    
      routes.MapRoute("BlogbyPageSortandFilter", "Blog/Page/{page}/CurrentFilter/{currentFilter}/SortBy/{sort}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("BlogbyPageandSort", "Blog/Page/{page}/SortBy/{sort}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("BlogbyPageandFilter", "Blog/Page/{page}/CurrentFilter/{currentFilter}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("BlogbySortandFilter", "Blog/SortBy/{sort}/CurrentFilter/{currentFilter}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("SortBlog", "Blog/SortBy/{sort}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("BlogbyPage", "Blog/Page/{page}", defaults: new { controller = "Blog", action = "Index" }); 
      routes.MapRoute("BlogbyFilter", "Blog/CurrentFilter/{currentFilter}", defaults: new { controller = "Blog", action = "Index" }); 

これらのルーティングは、ソーティングページング、Pagedlist.mvcを用いてフィルタリングするために使用されます。これらはすべて正常に動作しています。検索はルーティングのようにパラメータを渡すことはありません。パラメータをクエリ文字列として渡しています。

私はこれを解決するために手伝ってください。フォームのメソッドがフォームを送信する際に、型を取得するように設定されている場合は

ラリータ

+0

を持っていると仮定すると、モデル・ビュー・コントローラタグはパターンに関する質問のためであることに注意してください。 ASP.NET-MVCの実装には特定のタグがあります。 –

答えて

0

おかげで、フォームデータは、(?で始まる)クエリ文字列値としてaction属性のURLに追加されます。これはブラウザが行うことです。あなたのasp.net mvcのルーティングはこれで何もできません。

フォームが送信されるときに/Blog/SearchManufacturer/land URLが絶対に必要な場合は、クライアントサイドのJavaScriptでフォーム送信イベントをハイジャックして、必要に応じてURLを更新できます。以下のコードは、あなたが望む出力を提供します。

$(function() { 

    $("#searchFrm").submit(function (e) { 
     e.preventDefault(); 
     var formAction = $(this).attr("action"); 
     if (!formAction.endsWith('/')) { 
      formAction += '/'; 
     } 
     var url = formAction +'SearchManufacturer/'+ $("#SearchManufacturer").val(); 
     window.location.href = url; 
    }); 

}); 

フォームタグにid値「searchFrm」

@using (Html.BeginForm("Index", "Blog",FormMethod.Get,new { id="searchFrm"})) 
{ 
    // Your existing code goes here 
} 
+0

こんにちはShyju、答えをありがとう。いい説明。それは完璧に働いています。 – user3657053