csharp 有关如何使用app_code文件夹创建razorviews可以使用的可重用的html帮助程序的示例。这些将是.cshtml

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 有关如何使用app_code文件夹创建razorviews可以使用的可重用的html帮助程序的示例。这些将是.cshtml相关的知识,希望对你有一定的参考价值。

@*
 *  FILE  : /SiteElements/Razorviews/MyListingRazorview.cshtml
 *@

@{
  @*
   *  NOTES
   *  *****
   *  To call html helpers we refer to the file name and helper name:
   *  
   *  Example : FileName.HelperName();
   *@

  // query-string parameters
  int QS_PAGE = Request.QueryString["page"].AsInt() > 0 ? Request.QueryString["page"].AsInt() : 1;
  
  // calculated parameters
  int NUMBER_OF_PAGES = 20;   // this would normally be calculated by using the result count and a formula (below), but is hard-coded for this example
  
  // formula
  NUMBER_OF_PAGES = NUMBER_OF_RESULTS / DISPLAY;
  if(NUMBER_OF_RESULTS % DISPLAY != 0)
  {
    NUMBER_OF_PAGES = NUMBER_OF_PAGES + 1;
  }
  START_INDEX = (QS_PAGE * DISPLAY) - DISPLAY;

  @HtmlHelpers.RenderPagination(QS_PAGE, NUMBER_OF_PAGES)
}
@*
 *  FILE  : /App_Code/HtmlHelpers.cshtml
 *@

@using System.Collections.Specialized

@helper RenderPagination(int pageNumber, int pageCount)
{
  @*
   *  HELPER : RENDER PAGINATION
   *  **************************
   *  Description : takes an input 'current page number' and 'page count' to 
   *                work at what point in the pagination we are at and the 
   *                pagination size
   *  
   *  Example use : a site that has multiple custom listing razorviews (search, 
   *                news, events etc) that all shared the same html structure 
   *                for pagination and query-string parameters
   *@

  <ul>
      @{
          // previous link
          if(pageNumber > 1)
          {
              // url
              NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
              qs.Set("page", (pageNumber - 1).ToString());
              string url = Request.Url.AbsolutePath + "?" + qs.ToString();
              
              <li class="previous">
                  <a href="@url" title="Previous page">&lsaquo;</a>
              </li>
          }
          
          // page links
          for(int i = 1; i <= pageCount; i++)
          {
              int rangeMin = pageNumber - 4;
              int rangeMax = pageNumber + 4;
              
              if(i >= rangeMin && i <= rangeMax)
              {
                  // css
                  string css = (i == pageNumber) ? "selected" : null;
                  
                  // url
                  NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
                  qs.Set("page", i.ToString());
                  string url = Request.Url.AbsolutePath + "?" + qs.ToString();
                  
                  <li class="@css">
                      <a href="@url">@i</a>
                  </li>
              }
          }
          
          // next link
          if (pageNumber < pageCount)
          {
              // url
              NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
              qs.Set("page", (pageNumber + 1).ToString());
              string url = Request.Url.AbsolutePath + "?" + qs.ToString();
              
              <li class="next">
                  <a href="@url" title="Next page">&rsaquo;</a>
              </li>
          }
      }
  </ul>
}

以上是关于csharp 有关如何使用app_code文件夹创建razorviews可以使用的可重用的html帮助程序的示例。这些将是.cshtml的主要内容,如果未能解决你的问题,请参考以下文章

csharp 通过反射从App_Code获取类型。 http://stackoverflow.com/questions/331564/how-to-use-reflection-to-create-

我们可以在控制台应用程序中有一个 app_code 文件夹吗?

文件夹 App_Code 内的 C# 访问类

App_Code 文件夹,忽略编译错误?

网站和 App_Code 无法正常工作 (#C)

csharp 使用游戏对象上下文在GameObject上创建子容器