2017-04-10 16 views
0

私がしようとしているのは、「検索文字列」として機能する入力ボックスと、検索アクションへのPOSTです。同じ画面で結果を表示したいのですが、例外はありますが、Pageingコントロールを使用しようとすると、 "@ html.PagedList。そのコントロールは常に" [httpGet] Search notポスト。ここで@ url.Action投稿で@Html.PagedListPageを使用することはできますか

は、コードは次のとおりです。

public ActionResult Search() 
    { 
     return View(); 
    } 

[HttpPost] 
public ActionResult Search(FormCollection fc, int? pageNumber) 
{ 
    var searchString = fc["searchString"]; 
    var results = new ArrayList(); 
    var mylist = new List<SearchResult>(); 
    var model = new SearchViewModel(); 

    // Search Communities 
    var search = from s in db.Communities 
       where s.ComunityName.Contains(searchString) || s.Description.Contains(searchString) 
       select s; 

    // Search Resource Center 
    var docs = from d in db.ResourceCenters 
       where d.Title.Contains(searchString) || d.Description.Contains(searchString) 
       select d; 

    // Set up Arraylist with type Community 
    foreach(var c in search) 
    { 
     var community = new SearchResult(); 
     community.type = "community"; 
     community.CommunityId = c.CommunityId; 
     community.CommunityName = c.ComunityName; 
     community.Description = c.Description; 
     community.CommunityType = c.CommunityType1.TypeName; 
     community.CommunityCity = c.CommunityCity; 
     community.CommunityState = c.CommunityState; 
     community.CommunityZip = c.CommunityZip; 
     community.Population = c.Population; 
     mylist.Add(community); 
    } 

    // Set up ArrayList with type ResourceCenter 
    foreach (var d in docs) 
    { 
     var document = new SearchResult(); 
     document.type = "document"; 
     document.Title = d.Title; 
     document.Document_Description = d.Description; 
     document.FilePath = d.FilePath; 
     document.Date = Convert.ToDateTime(d.Date); 
     document.UpLoadedBy = d.UpLoadedBy; 
     mylist.Add(document); 
    } 

    model.results = mylist; 
    ViewBag.results = model.results; 
    ViewBag.searchString = searchString; 

    return View(mylist.ToPagedList(pageNumber ?? 1, 5)); 

とマークアップ:

@using (Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    <input type="text" id="searchString" name="searchString" class="form-control" required="required" /> 
    <input type="submit" class="btn btn-default" value="Search" /> 
    <hr /> 


    if (@Model != null) 
    { 
     if (@Model.Count != 0) 
     { 
      <h3>The following results were found for @ViewBag.searchString</h3> 


      foreach (var search in @Model) 
      { 

       if (@search.type == "community") 
       { 
        <div class="resource-element"> 
         <a href="/Communities/CommunityPage/@search.CommunityId"> 
          <span class="resource-type pull-right">Community</span> 
         </a> 
         <a href="/Communities/CommunityPage/@search.CommunityId"><h3>@search.CommunityName</h3></a> 
         <p>@search.Description</p> 
         <span class="">Type : @search.CommunityType</span><br /> 
         <span class="">@search.CommunityCity, @search.CommunityState @search.CommunityZip</span><br /> 
         <span class="">Population: @search.Population</span> 
         <br> 
        </div> 
       } 
       else 
       { 


        <div class="resource-element"> 
         <a href="@search.FilePath"> 
          <span class="resource-type pull-right">Document</span> 
         </a> 
         <a href="@search.FilePath"><h3>@search.Title</h3></a> 
         <p>@search.Document_Description</p> 
         <span class="">@search.Date</span> 
         <br> 
         <span class="">@search.UpLoadedBy</span> 
         <br> 
        </div> 
       } 

      } 

      @Html.PagedListPager(Model, pageNumber => Url.Action("Search", "Home", new { pageNumber }), 
      new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true }) 

     } 
     else 
     { 

      if (@Model.Count == 0) 
      { 
       <div class="resource-element"> 
        <a href="#"> 
         <span class="resource-type pull-right"></span> 
        </a> 
        <h3>No Results Found</h3> 
       </div> 

      } 


     } 
    } 

} 

答えて

1

Url.Action<a>タグとしてレンダリングするので、それが戻ってあなたのGETアクションになるだろう。 <a>リンクは常にGETを実行します。その周りに方法はありません(<a>リンククリックイベントを無効にするJavaScriptを導入する以外)。そして、実際には、それはあなたがこれを解決する方法です。

  1. "pageNumber"のフォームに非表示フィールドを追加します。
  2. Url.Actionの代わりに、onclickイベントのリンクまたはボタンをレンダリングするだけです。
  3. そのリンクまたはボタンのonclickイベントでは、非表示の「pageNumber」値をインクリメントしてから、javascriptでフォームを送信してください。

希望があれば。

+0

ありがとうございます...私はそれを行うことができます –

関連する問題