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:총 데이터 페이지 수

第二步:뷰 추가(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>
    }

세 번째 단계:작업의 뷰 컨트롤러 수정

public ViewResult Index(int?63; pageIndex)
    }
      int pageInd = pageIndex.HasValue ?63; 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));
    }

第四步:페이지 호출(마지막 단계)

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

일반적으로 데이터는 변동됩니다. 

데이터가 제출될 때

 第一步:PageIndex라는 이름의 Controller를 만들어서 다음과 같은 메서드를 정의합니다: 

    public ActionResult PageIndexKey(int currentPage, int pageCount)
    }
      ViewBag.PageCount = pageCount;//총 데이터 페이지 수를 가져와서 페이지 뷰 페이지에 전달
      ViewBag.CurrentPage = currentPage;//현재 페이지 번호를 가져와서 페이지 뷰 페이지에 전달
      return PartialView();
    }

第二步:데이터 분포 뷰 생성

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

세 번째 단계:뷰와 컨트롤러 수정 작업

public ViewResult Index(int?63; pageIndex ,string search)
  }
  int pageInd = pageIndex.HasValue ?63; 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 })
 
} 

예제: 

    //데이터, 하나의 list의 집합 
    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#w 이메일을 통해 알려주시기 바랍니다.3codebox.com에 대한 신고를 위해 이메일을 보내주세요. (#을 @으로 변경하십시오.) 관련 증거를 제공하고, 사실이 확인되면 이 사이트는 즉시 위반된 내용을 삭제합니다.

좋아하는 것