Donate

Alphabetical Paging in ASP.NET MVC (C#)

Here's the C# version of Alphabetic Paging in VB.NET Posts:
1. Alphabetical Paging in ASP.NET MVC
2. Alphabetical-Paging-in-ASP-NET-MVC Source Code
Solution Structure
Alphabetical Paging in ASP.NET MVC (C#)
HtmlHelpers.cs
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Text;  
 using System.Web.Mvc;  
 namespace MVCAlphabeticPager.Helpers  
 {  
   public static class HtmlHelpers  
   {  
     public static HtmlString AlphabeticalPager(this HtmlHelper html, string selectedLetter, IEnumerable<string> firstLetters, Func<string, string> pageLink)  
     {  
       var sb = new StringBuilder();  
       var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());  
       var alphabet = Enumerable.Range(65, 26).Select(i => ((char)i).ToString()).ToList();  
       alphabet.Insert(0, "All");  
       alphabet.Insert(1, "0-9");  
       var ul = new TagBuilder("ul");  
       ul.AddCssClass("pagination");  
       ul.AddCssClass("alpha");  
       foreach (var letter in alphabet)  
       {  
         var li = new TagBuilder("li");  
         if (firstLetters.Contains(letter) || (firstLetters.Intersect(numbers).Any() && letter == "0-9") || letter == "All")  
         {  
           if (selectedLetter == letter || string.IsNullOrEmpty(selectedLetter) && letter == "All")  
           {  
             li.AddCssClass("active");  
             var span = new TagBuilder("span");  
             span.SetInnerText(letter);  
             li.InnerHtml = span.ToString();  
           }  
           else  
           {  
             var a = new TagBuilder("a");  
             a.MergeAttribute("href", pageLink(letter));  
             a.InnerHtml = letter;  
             li.InnerHtml = a.ToString();  
           }  
         }  
         else  
         {  
           li.AddCssClass("inactive");  
           var span = new TagBuilder("span");  
           span.SetInnerText(letter);  
           li.InnerHtml = span.ToString();  
         }  
         sb.Append(li.ToString());  
       }  
       ul.InnerHtml = sb.ToString();  
       return new HtmlString(ul.ToString());  
     }  
   }  
 }  
HomeController.cs
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.Mvc;  
 using MVCAlphabeticPager.Models;  
 namespace MVCAlphabeticPager.Controllers  
 {  
   public class HomeController : Controller  
   {  
     public ActionResult Index(string selectedLetter)  
     {  
       var model = new AlphabeticalPagingViewModel();  
       model.SelectedLetter = selectedLetter ;  
       using (var context = new AdventureWorks2012Entities())  
       {  
         model.FirstLetters = context.Products  
           .GroupBy(p => p.Name.Substring(0, 1))  
           .Select(x => x.Key.ToUpper())  
           .ToList();  
         //initialize model  
         model.Products = new List<ProductModel>();  
         if (string.IsNullOrEmpty(selectedLetter) || selectedLetter == "All")  
         {  
           model.Products  
             .AddRange(  
               (from item in context.Products  
                join category in context.ProductSubcategories  
                 on item.ProductSubcategoryID equals category.ProductSubcategoryID into ProductCategory  
                 from category in ProductCategory.DefaultIfEmpty()                       
                 select new ProductModel()   
                 {  
                   ProductName = item.Name,  
                   ProductID = item.ProductID,  
                   ProductNumber = item.ProductNumber,  
                   Color = (string.IsNullOrEmpty(item.Color)) ? "NA" : item.Color,  
                   StandardCost = item.StandardCost,  
                   ProductCategory = (string.IsNullOrEmpty(category.Name)) ? "NA" : category.Name  
                 }).ToList());   
         }  
         else  
         {  
           if (selectedLetter == "0-9")  
           {  
             var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());  
             model.Products  
               .AddRange(  
                 (from item in context.Products  
                   .Where(item => numbers.Contains(item.Name.Substring(0, 1)))  
                 join category in context.ProductSubcategories  
                   on item.ProductSubcategoryID equals category.ProductSubcategoryID into ProductCategory  
                   from category in ProductCategory.DefaultIfEmpty()                
                 select new ProductModel()  
                 {  
                   ProductName = item.Name,  
                   ProductID = item.ProductID,  
                   ProductNumber = item.ProductNumber,  
                   Color = (string.IsNullOrEmpty(item.Color)) ? "NA" : item.Color,  
                   StandardCost = item.StandardCost,  
                   ProductCategory = (string.IsNullOrEmpty(category.Name)) ? "NA" : category.Name  
                 }).ToList());   
           }  
           else  
           {  
             model.Products  
               .AddRange(  
                 (from item in context.Products  
                   .Where(item => item.Name.Trim().StartsWith(selectedLetter))  
                 join category in context.ProductSubcategories  
                   on item.ProductSubcategoryID equals category.ProductSubcategoryID into ProductCategory  
                   from category in ProductCategory.DefaultIfEmpty()  
                 select new ProductModel()  
                 {  
                   ProductName = item.Name,  
                   ProductID = item.ProductID,  
                   ProductNumber = item.ProductNumber,  
                   Color = (string.IsNullOrEmpty(item.Color)) ? "NA" : item.Color,  
                   StandardCost = item.StandardCost,  
                   ProductCategory = (string.IsNullOrEmpty(category.Name)) ? "NA" : category.Name  
                 }).ToList());   
           }  
         }  
       }  
       return View(model);  
     }  
     public ActionResult About()  
     {  
       ViewBag.Message = "Your application description page.";  
       return View();  
     }  
     public ActionResult Contact()  
     {  
       ViewBag.Message = "Your contact page.";  
       return View();  
     }  
   }  
 }  
ProductModel.cs
 public class ProductModel  
   {  
     public int ProductID { get; set; }  
     public string ProductNumber { get; set; }  
     public string ProductName { get; set; }  
     public decimal StandardCost { get; set; }  
     public string Color { get; set; }  
     public string ProductCategory { get; set; }  
   }  
AlphabeticalPagingViewModel.cs
 public class AlphabeticalPagingViewModel  
   {  
     public List<ProductModel> Products { get; set; }      
     public List<string> FirstLetters { get; set; }  
     public string SelectedLetter { get; set; }      
   }  
Index.cshtml
@model MVCAlphabeticPager.Models.AlphabeticalPagingViewModel   
 <br />  
 <div class="panel panel-primary">    
   <div class="panel-heading panel-head">Product Listing</div>  
   <div class="panel-body">      
     @Html.AlphabeticalPager(Model.SelectedLetter, Model.FirstLetters, x => Url.Action("Index", new { selectedLetter = x }))  
     <table class="table" style="margin: 4px">  
       <tr>  
         <th>  
           @Html.DisplayName("Product ID")  
         </th>  
         <th>  
           @Html.DisplayName("Product Number")  
         </th>  
         <th>  
           @Html.DisplayName("Product Name")  
         </th>  
         <th>  
           @Html.DisplayName("Standard Cost")  
         </th>  
         <th>  
           @Html.DisplayName("Color")  
         </th>  
         <th>  
           @Html.DisplayName("Category")  
         </th>  
       </tr>  
       @foreach (var item in Model.Products)  
       {  
         <tr>  
           <td>  
             @Html.DisplayFor(modelItem => item.ProductID)   
           </td>  
           <td>  
             @Html.DisplayFor(modelItem => item.ProductNumber)  
           </td>  
           <td>  
             @Html.DisplayFor(modelItem => item.ProductName)  
           </td>  
           <td>  
             @Html.DisplayFor(modelItem => item.StandardCost)  
           </td>  
           <td>  
             @Html.DisplayFor(modelItem => item.Color)  
           </td>  
           <td>  
             @Html.DisplayFor(modelItem => item.ProductCategory)  
           </td>  
         </tr>  
       }  
     </table>  
     @Html.AlphabeticalPager(Model.SelectedLetter, Model.FirstLetters, x => Url.Action("Index", new { selectedLetter = x }))  
   </div>  
 </div>  

Cheers! :)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.