English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

ASP.NET MVC4 Razorテンプレートの簡単な分ページ効果

一、データなしの提出

第1歩、空のコントローラーとして「PageIndex」を作成し、以下のようにメソッドをカスタマイズします:   

    public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
    }
      //int count = db.Product.Count();
      ViewBag.PageCount = pageCount;//操作から全データページ数を取得し、ページビューに渡します
      ViewBag.CurrentPage = currentPage;//操作から現在のページ番号を取得し、ページビューに渡します
      ViewBag.action = action;
      ViewBag.controller = controller;
      return PartialView();
    }

四つのパラメータを传入します: 

action:アクション(ページングするビューのアクション、デフォルトはIndex);

controller:コントローラー;

currentPage:現在のページ数;

pageCount:データの全ページ数

第2ステップ:ビューを追加(PageIndex)

@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
    }
      <span>現在、データが表示されていません!</span>
    }
    else
    }
      if (ViewBag.CurrentPage <= 10)
    }
    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
    ホーム</a>|</span>
    }
  else
  }
  <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
    ホーム</a>
  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">
    ...</a> </span>
  }
  for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
  }
    if (i <= 0)
    }
      continue;
    }
    if (i > ViewBag.PageCount)
    }
      break;
    }
  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">
    第 @i ページ</a>|</span>
  }
  if (ViewBag.CurrentPage > 1)
  }
  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">
    前のページ</a>|</span>
  }
  if (ViewBag.PageCount > ViewBag.CurrentPage)
  }
  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">
    次のページ</a></span>
  }
  if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
  }
  <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
    最後のページ</a>
  }
  else
  }
  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">
    ...</a></span>
  <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
    最後のページ</a>
  }
  <span style="padding-left: 20px">現在のページ: @ViewBag.CurrentPage | 総 @ViewBag.PageCount ページ
  </span>
    }

第3ステップ:操作のビューのコントローラーを変更

public ViewResult Index(int? pageIndex)
    }
      int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
       ViewBag.PageCount = (int)Math.Ceiling(result.Count()) / 20.0);
      //ここではtake、ページごとに20件表示
      return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
    }

第4ステップ:ページ呼び出し(最終ステップ)

@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

一般的には、データは変動します。 

データの提出があります

 第1ステップ:PageIndexという名前の空のコントローラーを作成し、以下のメソッドをカスタマイズします: 

    public ActionResult PageIndexKey(int currentPage, int pageCount)
    }
      ViewBag.PageCount = pageCount;//操作から全データページ数を取得し、ページビューに渡します
      ViewBag.CurrentPage = currentPage;//操作から現在のページ番号を取得し、ページビューに渡します
      return PartialView();
    }

第2ステップ:分布ビューの作成

 <script>
  $(function () {
    $("#pageingByForm a").click(function (event) {
      $("#pageIndex").val($(this).attr("pageIndex"));
      //$(this).parent("Form").submit();
      document.getElementsByTagName("Form").item(0).submit();
      event.preventDefault();
    });
  });
</script>
@Html.Hidden("pageIndex")
<div id="pageingByForm">
  @if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
  }
    <span>現在データはありません</span>
  }
  else
  }
    if (ViewBag.CurrentPage <= 10)
    }
    <span><a pageindex="1" href="#">最初のページ</a>|</span>
    }
    else
    }
    <span><a pageindex="1" href="#">最初のページ</a>|</span>
    <span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>
    }
    for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
    }
      if (i <= 0)
      }
        continue;
      }
      if (i > ViewBag.PageCount)
      }
        break;
      }
    <span><a pageIndex="@i" href="#">第 @i ページ</a>|</span>
    }
    if (ViewBag.CurrentPage >1)
    }
    <span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">前のページ</a>|</span>
    }
    if (ViewBag.PageCount > ViewBag.CurrentPage)
    }
    <span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">次のページ</a></span>
    }
    if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
    }
    }
    else
    }
    <span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>
    <span><a pageIndex="@ViewBag.PageCount" href="#">最終ページ</a></span>
    }
    <span style="padding-left: 20px">現在のページ: @ViewBag.CurrentPage | 総 @ViewBag.PageCount ページ
    </span>
  }
</div>

第3ステップ:操作ビューとコントローラの修正

public ViewResult Index(int? pageIndex ,string search)
  }
  int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
   ViewBag.PageCount = (int)Math.Ceiling(result.Count()) / 20.0); 
  return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
  }

ビュー(ページ呼び出し):
 @using (Html.BeginForm())
} 

性別に基づいて検索結果を取得 

性別: @Html.TextBox("sex")

<input type="submit" value="検索" />  

@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
 
} 

Example: 

    //データ、リストの集合 
    List<string> s = new List<string>(); 
      s.Add("張軍"); 
      ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0); 
      return View(s.Skip((pageInd - 1) * 20).Take(20)); 
    @Html.Action("PageIndex", "PageIndex", 
    new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

これでこの記事のすべての内容が終わります。皆さんの学習に役立つことを願っています。また、ナイアラガイドを多くのサポートをお願いします。

声明:この記事の内容はインターネットから取得しており、著作権者は所有しており、インターネットユーザーによって自発的に貢献し、自己でアップロードされています。このウェブサイトは所有権を持ちません。また、人間による編集は行われておらず、関連する法的責任も負いません。著作権侵害を疑われる内容が見つかった場合は、メールを送信して:notice#oldtoolbag.com(メールを送信する際に、#を@に置き換えてください。報告を行い、関連する証拠を提供してください。一旦確認がとりあえず、このサイトは即座に侵害を疑われるコンテンツを削除します。)

おすすめ