├── WebGridExample ├── Views │ ├── _ViewStart.cshtml │ ├── Shared │ │ ├── Error.cshtml │ │ ├── _Layout.cshtml │ │ ├── _webGridFilter.cshtml │ │ ├── DisplayTemplates │ │ │ └── BootstrapPagination.cshtml │ │ └── UserGrid.cshtml │ ├── Web.config │ └── User │ │ ├── WebGridBatch.cshtml │ │ ├── Index.cshtml │ │ ├── WebGridExcel.cshtml │ │ ├── WebGridCSV.cshtml │ │ ├── WebGridExcelFiltering.cshtml │ │ ├── WebGridLLWebApi.cshtml │ │ ├── WebGridLLSignalR.cshtml │ │ ├── WebGridInlineEditing.cshtml │ │ └── WebGridValidating.cshtml ├── Global.asax ├── App_Data │ ├── UserDatabase.mdf │ └── UserDatabase_log.ldf ├── fonts │ ├── glyphicons-halflings-regular.eot │ ├── glyphicons-halflings-regular.ttf │ ├── glyphicons-halflings-regular.woff │ └── glyphicons-halflings-regular.woff2 ├── ViewModel │ ├── WebGridExcelViewModel.cs │ ├── WebGridWebApiViewModel.cs │ ├── WebGridBatchViewModel.cs │ ├── WebGridFilterModel.cs │ ├── BaseViewModel.cs │ ├── PagingModel.cs │ ├── UserViewModel.cs │ └── ExportParameters.cs ├── FormCommands │ ├── CommandList.cs │ ├── SetPageRowsCommand.cs │ ├── SendEmailCommand.cs │ ├── FormCommand.cs │ └── DeleteUserCommand.cs ├── Startup.cs ├── Models │ ├── ValidationMessage.cs │ ├── WebGridFilterModel.cs │ ├── UserConfiguration.cs │ ├── WebGridModel.cs │ ├── User.cs │ └── UserContext.cs ├── App_Start │ ├── FilterConfig.cs │ ├── RouteConfig.cs │ ├── WebApiConfig.cs │ └── BundleConfig.cs ├── Interface │ ├── IUserContext.cs │ ├── IFormCommand.cs │ ├── IUserRepository.cs │ └── IRepository.cs ├── Helpers │ ├── Url │ │ └── UrlHelperExtensions.cs │ └── Html │ │ ├── EditableHelpers.cs │ │ ├── WebGridHelpers.cs │ │ └── PagingExtensions.cs ├── Global.asax.cs ├── CommandResults │ └── UserCommandResult.cs ├── Api │ └── UserController.cs ├── ModelBinders │ ├── UserViewModelBinder.cs │ ├── PagingBinder.cs │ └── ExportViewModelBinder.cs ├── ActionResults │ ├── CsvResult.cs │ ├── ExcelResult.cs │ ├── CommandResult.cs │ └── BatchCommandResult.cs ├── Repository │ ├── UserRepository.cs │ └── Repository.cs ├── Web.Debug.config ├── Formatters │ ├── CsvHelpers.cs │ └── ExcelFormatter.cs ├── Web.Release.config ├── Properties │ └── AssemblyInfo.cs ├── Hubs │ └── WebGridHub.cs ├── Scripts │ └── WebGridFilter.js ├── packages.config ├── Web.config ├── WebGridResources.Designer.cs ├── Controllers │ └── UserController.cs ├── Resources │ ├── CommonResources.Designer.cs │ ├── CommonResources.resx │ └── HelpersResources.resx ├── WebGridResources.resx └── WebGridExample.csproj ├── WebGridExample.sln ├── README.md ├── .gitattributes └── .gitignore /WebGridExample/Views/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "~/Views/Shared/_Layout.cshtml"; 3 | } 4 | -------------------------------------------------------------------------------- /WebGridExample/Global.asax: -------------------------------------------------------------------------------- 1 | <%@ Application Codebehind="Global.asax.cs" Inherits="WebGridExample.MvcApplication" Language="C#" %> 2 | -------------------------------------------------------------------------------- /WebGridExample/App_Data/UserDatabase.mdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/App_Data/UserDatabase.mdf -------------------------------------------------------------------------------- /WebGridExample/App_Data/UserDatabase_log.ldf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/App_Data/UserDatabase_log.ldf -------------------------------------------------------------------------------- /WebGridExample/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /WebGridExample/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /WebGridExample/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /WebGridExample/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdanylko/WebGridExample/HEAD/WebGridExample/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /WebGridExample/ViewModel/WebGridExcelViewModel.cs: -------------------------------------------------------------------------------- 1 | namespace WebGridExample.ViewModel 2 | { 3 | public class WebGridExcelViewModel : WebGridBatchViewModel 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /WebGridExample/FormCommands/CommandList.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace WebGridExample.FormCommands 4 | { 5 | public class CommandList : List { } 6 | } -------------------------------------------------------------------------------- /WebGridExample/Startup.cs: -------------------------------------------------------------------------------- 1 | using Owin; 2 | 3 | namespace WebGridExample 4 | { 5 | public class Startup 6 | { 7 | public void Configuration(IAppBuilder app) 8 | { 9 | app.MapSignalR(); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /WebGridExample/Models/ValidationMessage.cs: -------------------------------------------------------------------------------- 1 | namespace WebGridExample.Models 2 | { 3 | public class ValidationMessage 4 | { 5 | public string Message { get; set; } 6 | public string MemberName { get; set; } 7 | public bool Success { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /WebGridExample/ViewModel/WebGridWebApiViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using MvcPaging; 3 | using WebGridExample.Models; 4 | 5 | namespace WebGridExample.ViewModel 6 | { 7 | public class WebGridWebApiViewModel : WebGridBatchViewModel 8 | { 9 | } 10 | } -------------------------------------------------------------------------------- /WebGridExample/App_Start/FilterConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | 3 | namespace WebGridExample 4 | { 5 | public class FilterConfig 6 | { 7 | public static void RegisterGlobalFilters(GlobalFilterCollection filters) 8 | { 9 | filters.Add(new HandleErrorAttribute()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /WebGridExample/ViewModel/WebGridBatchViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using WebGridExample.Models; 3 | namespace WebGridExample.ViewModel 4 | { 5 | public class WebGridBatchViewModel: BaseViewModel 6 | { 7 | public IEnumerable Users { get; set; } 8 | public User User { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /WebGridExample/Interface/IUserContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Entity; 3 | using WebGridExample.Models; 4 | 5 | namespace WebGridExample.Interface 6 | { 7 | public interface IUserContext : IDisposable 8 | { 9 | IDbSet Users { get; set; } // Users 10 | 11 | int SaveChanges(); 12 | 13 | // Stored Procedures 14 | } 15 | } -------------------------------------------------------------------------------- /WebGridExample/Interface/IFormCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | 3 | namespace WebGridExample.Interface 4 | { 5 | public interface IFormCommand 6 | { 7 | string CommandName { get; set; } 8 | string Result { get; set; } 9 | bool Success { get; set; } 10 | ControllerContext Context { get; set; } 11 | bool Execute(string input); 12 | } 13 | } -------------------------------------------------------------------------------- /WebGridExample/Views/Shared/Error.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = null; 3 | } 4 | 5 | 6 | 7 | 8 | 9 | Error 10 | 11 | 12 |
13 |

Error.

14 |

An error occurred while processing your request.

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /WebGridExample/ViewModel/WebGridFilterModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using WebGridExample.Models; 4 | 5 | namespace WebGridExample.ViewModel 6 | { 7 | public class WebGridFilterModel 8 | { 9 | public IEnumerable Users { get; set; } 10 | public string HeadingText { get; set; } 11 | public Func Property { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /WebGridExample/Helpers/Url/UrlHelperExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | using Microsoft.Owin; 3 | using WebGridExample.Interface; 4 | 5 | namespace WebGridExample.Helpers.Url 6 | { 7 | public static class UrlHelperExtensions 8 | { 9 | public static string MainUrl(this UrlHelper helper, int size) 10 | { 11 | return helper.Content(string.Format("~/?size={0}", size)); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /WebGridExample/ViewModel/BaseViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Web.Mvc; 3 | using WebGridExample.ModelBinders; 4 | using WebGridExample.Models; 5 | 6 | namespace WebGridExample.ViewModel 7 | { 8 | [ModelBinder(typeof(WebGridModelBinder))] 9 | public class BaseViewModel 10 | { 11 | public IEnumerable SelectedUsers { get; set; } 12 | public bool Delete { get; set; } 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /WebGridExample/Models/WebGridFilterModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace WebGridExample.Models 8 | { 9 | public class WebGridFilterModel 10 | { 11 | public IEnumerable Users { get; set; } 12 | public string HeadingText { get; set; } 13 | public Func Property { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /WebGridExample/Interface/IUserRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using MvcPaging; 3 | using WebGridExample.Models; 4 | using WebGridExample.ViewModel; 5 | 6 | namespace WebGridExample.Interface 7 | { 8 | public interface IUserRepository: IRepository 9 | { 10 | User GetById(int id); 11 | IPagedList GetPagedUsers(PagingModel model); 12 | IEnumerable GetPagedUsers(int? page, int pageSize); 13 | } 14 | } -------------------------------------------------------------------------------- /WebGridExample/FormCommands/SetPageRowsCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | 3 | namespace WebGridExample.FormCommands 4 | { 5 | public class SetPageRowsCommand : FormCommand 6 | { 7 | public SetPageRowsCommand(ControllerContext context) 8 | { 9 | CommandName = "size"; 10 | Context = context; 11 | } 12 | public override bool Execute(string input) 13 | { 14 | Result = input; 15 | return true; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /WebGridExample/ViewModel/PagingModel.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Helpers; 2 | using System.Web.Mvc; 3 | using WebGridExample.ModelBinders; 4 | 5 | namespace WebGridExample.ViewModel 6 | { 7 | [ModelBinder(typeof(PagingBinder))] 8 | public class PagingModel 9 | { 10 | public int PageIndex { get; set; } 11 | public int PageSize { get; set; } 12 | public string Sort { get; set; } 13 | public SortDirection SortDir { get; set; } 14 | public int PageNumber { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /WebGridExample/ViewModel/UserViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Web.Mvc; 3 | using WebGridExample.ModelBinders; 4 | using WebGridExample.Models; 5 | 6 | namespace WebGridExample.ViewModel 7 | { 8 | public class UserViewModel 9 | { 10 | public IEnumerable Users { get; set; } 11 | public User User { get; set; } 12 | 13 | public IEnumerable SelectedUsers { get; set; } 14 | public bool Delete { get; set; } 15 | public bool SendEmail { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /WebGridExample/App_Start/RouteConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | using System.Web.Routing; 3 | 4 | namespace WebGridExample 5 | { 6 | public class RouteConfig 7 | { 8 | public static void RegisterRoutes(RouteCollection routes) 9 | { 10 | routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 11 | 12 | routes.MapRoute( 13 | name: "Default", 14 | url: "{controller}/{action}/{id}", 15 | defaults: new { controller = "User", action = "WebGridBatch", id = UrlParameter.Optional } 16 | ); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /WebGridExample/ViewModel/ExportParameters.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | using WebGridExample.ModelBinders; 3 | 4 | namespace WebGridExample.ViewModel 5 | { 6 | public enum RangeOptions { Current, All } 7 | public enum Output { Excel, Csv } 8 | 9 | [ModelBinder(typeof(ExportViewModelBinder))] 10 | public class ExportParameters 11 | { 12 | public RangeOptions Range { get; set; } 13 | public Output OutputType { get; set; } 14 | public int CurrentPage { get; set; } 15 | public int PageSize { get; set; } 16 | public bool PagingEnabled { get; set; } 17 | } 18 | } -------------------------------------------------------------------------------- /WebGridExample/Interface/IRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | 5 | namespace WebGridExample.Interface 6 | { 7 | public interface IRepository where TEntity : class 8 | { 9 | IQueryable GetAll(); 10 | IQueryable Find(Expression> predicate); 11 | int Count(Expression> predicate); 12 | int Add(TEntity entity); 13 | int SaveChanges(); 14 | int Delete(TEntity entity); 15 | TEntity First(Expression> predicate); 16 | void Dispose(); 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /WebGridExample/FormCommands/SendEmailCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web.Mvc; 3 | 4 | namespace WebGridExample.FormCommands 5 | { 6 | public class SendEmailCommand : FormCommand 7 | { 8 | public SendEmailCommand(ControllerContext context) : base(context) 9 | { 10 | CommandName = "btnSendEmail"; 11 | } 12 | public override bool Execute(string input) 13 | { 14 | if (String.IsNullOrEmpty(input)) return false; 15 | var userId = input; 16 | 17 | // EMailTemplateService.SendTemplatedEMail(userId); 18 | 19 | return true; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /WebGridExample/Global.asax.cs: -------------------------------------------------------------------------------- 1 | using System.Web; 2 | using System.Web.Http; 3 | using System.Web.Mvc; 4 | using System.Web.Optimization; 5 | using System.Web.Routing; 6 | 7 | namespace WebGridExample 8 | { 9 | public class MvcApplication : HttpApplication 10 | { 11 | protected void Application_Start() 12 | { 13 | AreaRegistration.RegisterAllAreas(); 14 | FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 15 | 16 | WebApiConfig.Register(GlobalConfiguration.Configuration); 17 | 18 | RouteConfig.RegisterRoutes(RouteTable.Routes); 19 | BundleConfig.RegisterBundles(BundleTable.Bundles); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WebGridExample/CommandResults/UserCommandResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web.Mvc; 3 | using WebGridExample.ActionResults; 4 | using WebGridExample.FormCommands; 5 | using WebGridExample.Interface; 6 | 7 | namespace WebGridExample.CommandResults 8 | { 9 | public class UserCommandResult : BatchCommandResult 10 | { 11 | public UserCommandResult(ControllerContext context, Func successResult) 12 | : base(context, successResult) 13 | { 14 | Commands.Clear(); 15 | Commands.Add(new DeleteUserCommand(context)); 16 | Commands.Add(new SendEmailCommand(context)); 17 | Commands.Add(new SetPageRowsCommand(context)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /WebGridExample/Views/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @model WebGridExample.ViewModel.WebGridBatchViewModel 3 | 4 | 5 | 6 | 7 | 8 | 9 | WebGrid Example 10 | 13 | 14 | 15 | 16 |
17 | @RenderBody() 18 |
19 |
20 |

© @DateTime.Now.Year - My ASP.NET Application

21 |
22 |
23 | 24 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /WebGridExample/FormCommands/FormCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | using WebGridExample.Interface; 3 | 4 | namespace WebGridExample.FormCommands 5 | { 6 | public abstract class FormCommand : IFormCommand 7 | { 8 | public string CommandName { get; set; } 9 | public string Result { get; set; } 10 | public bool Success { get; set; } 11 | public ControllerContext Context { get; set; } 12 | public virtual bool Execute(string input) { return true; } 13 | public virtual bool Execute(string input, string input2) { return true; } 14 | protected FormCommand() : this(null) { } 15 | protected FormCommand(ControllerContext context) 16 | { 17 | Context = context; 18 | Success = true; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /WebGridExample/Api/UserController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Web.Http; 3 | using WebGridExample.Interface; 4 | using WebGridExample.Models; 5 | using WebGridExample.Repository; 6 | 7 | namespace WebGridExample.Api 8 | { 9 | public class UserController : ApiController 10 | { 11 | private readonly IUserRepository _repository; 12 | public UserController() : this(new UserRepository()) { } 13 | public UserController(IUserRepository repository) 14 | { 15 | _repository = repository; 16 | } 17 | 18 | // GET api/ (Used for a test to make sure it works) 19 | public IEnumerable Get() 20 | { 21 | return new string[] { "value1", "value2" }; 22 | } 23 | // GET api//5 24 | public IEnumerable GetPaging(int page, int pageSize) 25 | { 26 | return _repository.GetPagedUsers(page, pageSize); 27 | } 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /WebGridExample/Models/UserConfiguration.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations.Schema; 2 | using System.Data.Entity.ModelConfiguration; 3 | 4 | namespace WebGridExample.Models 5 | { 6 | internal class UserConfiguration : EntityTypeConfiguration 7 | { 8 | public UserConfiguration(string schema = "dbo") 9 | { 10 | ToTable(schema + ".Users"); 11 | HasKey(x => x.Id); 12 | 13 | Property(x => x.Id).HasColumnName("Id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 14 | Property(x => x.UserName).HasColumnName("UserName").IsRequired().IsUnicode(false).HasMaxLength(15); 15 | Property(x => x.LastLogin).HasColumnName("LastLogin").IsRequired(); 16 | Property(x => x.FirstName).HasColumnName("FirstName").IsRequired().IsUnicode(false).HasMaxLength(20); 17 | Property(x => x.LastName).HasColumnName("LastName").IsRequired().IsUnicode(false).HasMaxLength(30); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /WebGridExample/Views/Shared/_webGridFilter.cshtml: -------------------------------------------------------------------------------- 1 | 2 | @model WebGridExample.Models.WebGridFilterModel 3 | -------------------------------------------------------------------------------- /WebGridExample/Views/Shared/DisplayTemplates/BootstrapPagination.cshtml: -------------------------------------------------------------------------------- 1 | @using WebGridExample.Helpers.Html 2 | @model PaginationModel 3 |
    4 | @foreach (var link in Model.PaginationLinks) 5 | { 6 | @BuildLink(link) 7 | } 8 |
9 | 10 | @helper BuildLink(PaginationLink link) 11 | { 12 | var liBuilder = new TagBuilder("li"); 13 | if (link.IsCurrent) 14 | { 15 | liBuilder.MergeAttribute("class", "active"); 16 | } 17 | if (!link.Active) 18 | { 19 | liBuilder.MergeAttribute("class", "disabled"); 20 | } 21 | var aBuilder = new TagBuilder("a"); 22 | aBuilder.MergeAttribute("href", link.Url ?? "#"); 23 | // Ajax support 24 | if (Model.AjaxOptions != null) 25 | { 26 | foreach (var ajaxOption in Model.AjaxOptions.ToUnobtrusiveHtmlAttributes()) 27 | { 28 | aBuilder.MergeAttribute(ajaxOption.Key, ajaxOption.Value.ToString(), true); 29 | } 30 | } 31 | aBuilder.SetInnerText(link.DisplayText); 32 | liBuilder.InnerHtml = aBuilder.ToString(); 33 | @Html.Raw(liBuilder.ToString()) 34 | } -------------------------------------------------------------------------------- /WebGridExample/App_Start/WebApiConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Web.Http; 3 | using Newtonsoft.Json; 4 | 5 | namespace WebGridExample 6 | { 7 | public class WebApiConfig 8 | { 9 | public static void Register(HttpConfiguration configuration) 10 | { 11 | configuration.Routes.MapHttpRoute("API Default", 12 | "api/{controller}/{id}", 13 | new { id = RouteParameter.Optional }); 14 | 15 | // Remove the xml so we get back JSON 16 | var appXmlType = configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); 17 | GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); 18 | 19 | // Remove the k__backingfield issue in the names. 20 | JsonSerializerSettings jSettings = new JsonSerializerSettings(); 21 | GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = jSettings; 22 | 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /WebGridExample.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebGridExample", "WebGridExample\WebGridExample.csproj", "{65AE6C8D-B95F-4217-91A4-6D04B8CBC340}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {65AE6C8D-B95F-4217-91A4-6D04B8CBC340}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {65AE6C8D-B95F-4217-91A4-6D04B8CBC340}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {65AE6C8D-B95F-4217-91A4-6D04B8CBC340}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {65AE6C8D-B95F-4217-91A4-6D04B8CBC340}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /WebGridExample/Models/WebGridModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Web; 4 | using System.Web.Helpers; 5 | 6 | namespace WebGridExample.Models 7 | { 8 | public class WebGridModel 9 | { 10 | public WebGrid WebGrid { get; set; } 11 | public HttpContextBase HttpContext { get; set; } 12 | public string TableStyle { get; set; } 13 | public string HeaderStyle { get; set; } 14 | public string FooterStyle { get; set; } 15 | public string RowStyle { get; set; } 16 | public string AlternatingRowStyle { get; set; } 17 | public string SelectedRowStyle { get; set; } 18 | public string Caption { get; set; } 19 | public bool DisplayHeader { get; set; } 20 | public bool FillEmptyRows { get; set; } 21 | public string EmptyRowCellValue { get; set; } 22 | public IEnumerable Columns { get; set; } 23 | public IEnumerable Exclusions { get; set; } 24 | public Func Footer { get; set; } 25 | public object HtmlAttributes { get; set; } 26 | } 27 | } -------------------------------------------------------------------------------- /WebGridExample/ModelBinders/UserViewModelBinder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web.Mvc; 5 | using WebGridExample.Models; 6 | using WebGridExample.ViewModel; 7 | 8 | namespace WebGridExample.ModelBinders 9 | { 10 | public class WebGridModelBinder : DefaultModelBinder 11 | { 12 | public override object BindModel(ControllerContext controllerContext, 13 | ModelBindingContext bindingContext) 14 | { 15 | var list = new List(); 16 | var request = controllerContext.HttpContext.Request; 17 | 18 | if (request.Form.AllKeys.Contains("select")) 19 | { 20 | var userIdList = request.Form.Get("select"); 21 | var idList = userIdList.Split(','); 22 | list.AddRange(idList); 23 | } 24 | 25 | return new WebGridBatchViewModel 26 | { 27 | SelectedUsers = list.Select(e => new User {Id = Convert.ToInt32(e)}), 28 | Delete = !String.IsNullOrEmpty(request.Form.Get("btnDelete")) 29 | }; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /WebGridExample/ModelBinders/PagingBinder.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Helpers; 2 | using System.Web.Mvc; 3 | using WebGridExample.ViewModel; 4 | 5 | namespace WebGridExample.ModelBinders 6 | { 7 | public class PagingBinder : DefaultModelBinder 8 | { 9 | public override object BindModel(ControllerContext controllerContext, 10 | ModelBindingContext bindingContext) 11 | { 12 | var request = controllerContext.HttpContext.Request; 13 | var pageNum = request.QueryString.Get("Page"); 14 | var size = request.QueryString.Get("Size"); 15 | 16 | int pageNumber; 17 | if (!int.TryParse(pageNum, out pageNumber)) 18 | pageNumber = 1; // Default Page 19 | 20 | var pageIndex = pageNumber - 1; 21 | 22 | int pageSize; 23 | if (!int.TryParse(size, out pageSize)) 24 | pageSize = 10; // Default 10 records 25 | 26 | return new PagingModel 27 | { 28 | PageIndex = pageIndex, 29 | PageSize = pageSize, 30 | PageNumber = pageNumber 31 | }; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /WebGridExample/ActionResults/CsvResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | using System.Web.Mvc; 5 | using WebGridExample.Formatters; 6 | using WebGridExample.Models; 7 | 8 | namespace WebGridExample.ActionResults 9 | { 10 | public class CsvResult : ActionResult 11 | { 12 | private readonly IQueryable _records; 13 | private readonly string _filename; 14 | 15 | public CsvResult(IQueryable records, string filename) 16 | { 17 | _records = records; 18 | _filename = filename; 19 | } 20 | 21 | public override void ExecuteResult(ControllerContext context) 22 | { 23 | var sb = new StringBuilder(); 24 | sb.Append(_records.ToCsv(",")); 25 | 26 | var response = context.HttpContext.Response; 27 | response.ContentType = "text/csv"; 28 | response.AddHeader("content-disposition", 29 | String.Format("attachment; filename={0}", _filename)); 30 | 31 | response.Write(sb.ToString()); 32 | 33 | response.Flush(); 34 | response.End(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /WebGridExample/Models/User.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | 5 | namespace WebGridExample.Models 6 | { 7 | [Serializable] 8 | public class User: IValidatableObject 9 | { 10 | public int Id { get; set; } // Id (Primary key) 11 | public string UserName { get; set; } // UserName 12 | public DateTime LastLogin { get; set; } // LastLogin 13 | public string FirstName { get; set; } // FirstName 14 | public string LastName { get; set; } // LastName 15 | 16 | public User() 17 | { 18 | LastLogin = System.DateTime.Now; 19 | } 20 | 21 | public IEnumerable Validate(ValidationContext validationContext) 22 | { 23 | var list = new List(); 24 | 25 | if (UserName.Length > 10) 26 | { 27 | var item = new ValidationResult("You cannot have a UserName with more than 10 characters", 28 | new[] { "UserName" }); 29 | list.Add(item); 30 | } 31 | 32 | return list; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /WebGridExample/FormCommands/DeleteUserCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web.Mvc; 3 | using WebGridExample.Repository; 4 | 5 | namespace WebGridExample.FormCommands 6 | { 7 | public class DeleteUserCommand : FormCommand 8 | { 9 | public DeleteUserCommand(ControllerContext context) : base(context) 10 | { 11 | CommandName = "btnDelete"; 12 | } 13 | public override bool Execute(string input) 14 | { 15 | if (String.IsNullOrEmpty(input)) return false; 16 | 17 | var repository = new UserRepository(); 18 | int userId; 19 | if (!Int32.TryParse(input, out userId)) 20 | { 21 | return false; 22 | } 23 | 24 | var user = repository.GetById(userId); 25 | if (user != null) 26 | { 27 | repository.Delete(user); 28 | } 29 | try 30 | { 31 | repository.SaveChanges(); 32 | Success = true; 33 | } 34 | catch 35 | { 36 | Success = false; 37 | } 38 | return Success; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /WebGridExample/Repository/UserRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.Entity; 3 | using System.Linq; 4 | using MvcPaging; 5 | using WebGridExample.Interface; 6 | using WebGridExample.Models; 7 | using WebGridExample.ViewModel; 8 | 9 | namespace WebGridExample.Repository 10 | { 11 | public class UserRepository: Repository, IUserRepository 12 | { 13 | public UserRepository() : this(new UserContext()) { } 14 | public UserRepository(DbContext context) : base(context) { } 15 | 16 | public User GetById(int id) 17 | { 18 | return First(e => e.Id == id); 19 | } 20 | 21 | public IPagedList GetPagedUsers(PagingModel model) 22 | { 23 | var records = GetAll() 24 | .OrderBy(e=> e.UserName); 25 | var total = records.Count(); 26 | return new PagedList(records, model.PageIndex, model.PageSize, total); 27 | } 28 | 29 | public IEnumerable GetPagedUsers(int? page, int pageSize) 30 | { 31 | var pageIndex = page - 1 ?? 0; 32 | return GetAll() 33 | .OrderBy(e => e.UserName) 34 | .Skip(pageIndex * pageSize) 35 | .Take(pageSize) 36 | .ToList(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /WebGridExample/ActionResults/ExcelResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Web.Mvc; 4 | 5 | namespace WebGridExample.ActionResults 6 | { 7 | public class ExcelResult : ActionResult 8 | { 9 | private readonly Stream _excelStream; 10 | private readonly String _fileName; 11 | 12 | public ExcelResult(byte[] excel, String fileName) 13 | { 14 | _excelStream = new MemoryStream(excel); 15 | _fileName = fileName; 16 | } 17 | 18 | public override void ExecuteResult(ControllerContext context) 19 | { 20 | if (context == null) 21 | { 22 | throw new ArgumentNullException("We need a context!"); 23 | } 24 | 25 | var response = context.HttpContext.Response; 26 | response.ContentType = "application/vnd.ms-excel"; 27 | response.AddHeader("content-disposition", "attachment; filename=" + _fileName); 28 | 29 | byte[] buffer = new byte[4096]; 30 | while (true) 31 | { 32 | var read = _excelStream.Read(buffer, 0, buffer.Length); 33 | if (read == 0) break; 34 | response.OutputStream.Write(buffer, 0, read); 35 | } 36 | 37 | response.End(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /WebGridExample/Models/UserContext.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Entity; 2 | using WebGridExample.Interface; 3 | 4 | namespace WebGridExample.Models 5 | { 6 | public class UserContext : DbContext, IUserContext 7 | { 8 | public IDbSet Users { get; set; } // Users 9 | 10 | static UserContext() 11 | { 12 | Database.SetInitializer(null); 13 | } 14 | 15 | public UserContext() 16 | : base("Name=UserContext") 17 | { 18 | } 19 | 20 | public UserContext(string connectionString) : base(connectionString) 21 | { 22 | } 23 | 24 | public UserContext(string connectionString, System.Data.Entity.Infrastructure.DbCompiledModel model) : base(connectionString, model) 25 | { 26 | } 27 | 28 | protected override void OnModelCreating(DbModelBuilder modelBuilder) 29 | { 30 | base.OnModelCreating(modelBuilder); 31 | 32 | modelBuilder.Configurations.Add(new UserConfiguration()); 33 | } 34 | 35 | public static DbModelBuilder CreateModel(DbModelBuilder modelBuilder, string schema) 36 | { 37 | modelBuilder.Configurations.Add(new UserConfiguration(schema)); 38 | return modelBuilder; 39 | } 40 | 41 | // Stored Procedures 42 | } 43 | } -------------------------------------------------------------------------------- /WebGridExample/Web.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /WebGridExample/Formatters/CsvHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace WebGridExample.Formatters 8 | { 9 | public static class CsvHelpers 10 | { 11 | public static string ToCsv(this IEnumerable objectlist, string separator) 12 | { 13 | Type t = typeof(T); 14 | PropertyInfo[] props = t.GetProperties(); 15 | 16 | string header = String.Join(separator, props.Select(f => f.Name).ToArray()); 17 | 18 | StringBuilder csvdata = new StringBuilder(); 19 | csvdata.AppendLine(header); 20 | 21 | foreach (var o in objectlist) 22 | csvdata.AppendLine(ToCsvFields(separator, props, o)); 23 | 24 | return csvdata.ToString(); 25 | } 26 | 27 | private static string ToCsvFields(string separator, PropertyInfo[] properties, object o) 28 | { 29 | var line = new StringBuilder(); 30 | 31 | foreach (var f in properties) 32 | { 33 | if (line.Length > 0) 34 | line.Append(separator); 35 | 36 | var x = f.GetValue(o); 37 | 38 | if (x != null) 39 | line.Append(x); 40 | } 41 | 42 | return line.ToString(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /WebGridExample/Web.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 30 | 31 | -------------------------------------------------------------------------------- /WebGridExample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("WebGridExample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("WebGridExample")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fdebf92f-102c-46e9-b9db-ffd5973688c0")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /WebGridExample/App_Start/BundleConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Optimization; 2 | 3 | namespace WebGridExample 4 | { 5 | public class BundleConfig 6 | { 7 | // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 8 | public static void RegisterBundles(BundleCollection bundles) 9 | { 10 | bundles.Add(new ScriptBundle("~/bundles/jquery").Include( 11 | "~/Scripts/jquery-{version}.js")); 12 | 13 | bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( 14 | "~/Scripts/jquery.validate*")); 15 | 16 | // Use the development version of Modernizr to develop with and learn from. Then, when you're 17 | // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. 18 | bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( 19 | "~/Scripts/modernizr-*")); 20 | 21 | bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( 22 | "~/Scripts/bootstrap.js", 23 | "~/Scripts/respond.js")); 24 | 25 | bundles.Add(new StyleBundle("~/Content/css").Include( 26 | "~/Content/bootstrap.css", 27 | "~/Content/site.css")); 28 | 29 | // Set EnableOptimizations to false for debugging. For more information, 30 | // visit http://go.microsoft.com/fwlink/?LinkId=301862 31 | BundleTable.EnableOptimizations = true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /WebGridExample/ActionResults/CommandResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Web.Mvc; 4 | using WebGridExample.FormCommands; 5 | using WebGridExample.Interface; 6 | 7 | namespace WebGridExample.ActionResults 8 | { 9 | public class CommandResult : ActionResult 10 | { 11 | public CommandList Commands { get; } = new CommandList(); 12 | 13 | public string BatchName { get; set; } 14 | 15 | public FormCollection FormCollection { get; set; } 16 | 17 | public Func SuccessResult; 18 | 19 | public CommandResult(ControllerContext context, Func successResult) 20 | : this(context, successResult, String.Empty) 21 | { } 22 | 23 | public CommandResult(ControllerContext context, Func successResult, 24 | string triggerName) 25 | { 26 | BatchName = triggerName; 27 | FormCollection = new FormCollection(context.HttpContext.Request.Form); 28 | SuccessResult = successResult; 29 | } 30 | public override void ExecuteResult(ControllerContext context) 31 | { 32 | var command = GetKnownCommand(); 33 | if (command != null) 34 | command.Execute(String.Empty); 35 | SuccessResult(command).ExecuteResult(context); 36 | } 37 | protected IFormCommand GetKnownCommand() 38 | { 39 | return Commands.FirstOrDefault( 40 | e => FormCollection.AllKeys.Any(f => f.Equals(e.CommandName))); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /WebGridExample/Views/Web.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebGridExample 2 | Initial MVC WebGrid 3 | 4 | This is the base setup for an enhanced WebGrid for ASP.NET MVC. 5 | 6 | Here is the series so far: 7 | 8 | 19 | 20 | If you have any features you want included in this WebGrid, post a comment on the site and we'll see if we can add the feature into it. 21 | -------------------------------------------------------------------------------- /WebGridExample/Hubs/WebGridHub.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using Microsoft.AspNet.SignalR; 5 | using Microsoft.AspNet.SignalR.Hubs; 6 | using WebGridExample.Models; 7 | using WebGridExample.Repository; 8 | 9 | namespace WebGridExample.Hubs 10 | { 11 | [HubName("webGridHub")] 12 | public class WebGridHub : Hub 13 | { 14 | private readonly UserRepository _repository; 15 | 16 | 17 | public WebGridHub() : this(new UserRepository()) { } 18 | 19 | public WebGridHub(UserRepository repository) 20 | { 21 | _repository = repository; 22 | } 23 | 24 | public Task GetPagedUsers(int page, int pageSize) 25 | { 26 | var records = _repository.GetPagedUsers(page, pageSize); 27 | return Clients.Caller.populateGrid(records); 28 | } 29 | 30 | public Task SaveRecord(User user) 31 | { 32 | var record = _repository.GetById(user.Id); 33 | record.UserName = user.UserName; 34 | record.FirstName = user.FirstName; 35 | record.LastName = user.LastName; 36 | record.LastLogin = user.LastLogin; 37 | 38 | _repository.SaveChanges(); 39 | 40 | return Clients.Caller.recordSaved(); 41 | } 42 | 43 | public ValidationMessage GetValidation(User user) 44 | { 45 | var message = new ValidationMessage(); 46 | 47 | var validations = user.Validate(new ValidationContext(user)); 48 | if (validations.Any()) 49 | { 50 | message.Message = validations.First().ErrorMessage; 51 | message.MemberName = validations.First().MemberNames.First(); 52 | } 53 | message.Success = !validations.Any(); 54 | return message; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /WebGridExample/ModelBinders/ExportViewModelBinder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web.Mvc; 3 | using WebGridExample.ViewModel; 4 | 5 | namespace WebGridExample.ModelBinders 6 | { 7 | public class ExportViewModelBinder : DefaultModelBinder 8 | { 9 | public override object BindModel(ControllerContext controllerContext, 10 | ModelBindingContext bindingContext) 11 | { 12 | var request = controllerContext.HttpContext.Request; 13 | 14 | // Range 15 | var range = request.Form.Get("pageOptions"); 16 | var pagingOptions = range == "pageCurrent" 17 | ? RangeOptions.Current 18 | : RangeOptions.All; 19 | 20 | // Output 21 | var output = request.Form.Get("exportType"); 22 | var outputType = Output.Excel; 23 | switch (output) 24 | { 25 | case "exportCsv": 26 | outputType = Output.Csv; 27 | break; 28 | } 29 | 30 | int currentPage; 31 | if (!Int32.TryParse(request.Form.Get("CurrentPage"), out currentPage)) 32 | { 33 | currentPage = 0; 34 | } 35 | 36 | int pageSize; 37 | if (!Int32.TryParse(request.Form.Get("RowsPerPage"), out pageSize)) 38 | { 39 | pageSize = 0; 40 | } 41 | 42 | bool pagingEnabled; 43 | if (!bool.TryParse(request.Form.Get("PagingEnabled"), out pagingEnabled)) 44 | { 45 | pagingEnabled = false; 46 | } 47 | 48 | return new ExportParameters 49 | { 50 | Range = pagingOptions, 51 | OutputType = outputType, 52 | CurrentPage = currentPage, 53 | PageSize = pageSize, 54 | PagingEnabled = pagingEnabled 55 | }; 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /WebGridExample/Scripts/WebGridFilter.js: -------------------------------------------------------------------------------- 1 | var options = []; 2 | 3 | $(function () { 4 | var $grid = $("#grid"); 5 | 6 | // Force the 'select all' checkbox on/off 7 | function updateSelectAll(anchor) { 8 | var parent = $(anchor).closest(".dropdown-menu"); 9 | // Are any checkboxes checked? 10 | var isAll = $(".filterbox:not(:checked)", parent).length === 0; 11 | setTimeout(function () { 12 | $("a[data-value='all'] :checkbox", parent).prop('checked', isAll); 13 | }, 0); 14 | } 15 | 16 | // 'Select All' [Click] 17 | $("a[data-value='all']").on("click", function () { 18 | var $parent = $(this).closest(".dropdown-menu"), 19 | isChecked = $(":checkbox", this).is(":checked"); 20 | $(":checkbox", this).prop("checked", !isChecked); 21 | $(".filterbox", $parent).prop("checked", !isChecked); 22 | $("td:nth-child(1)", $grid).closest("tr").toggle(!isChecked); 23 | }); 24 | 25 | // Filter Checkbox 26 | $('.dropdown-menu a', $grid).on('click', function (event) { 27 | var self = this; 28 | var $target = $(event.currentTarget), 29 | id = $target.attr('data-value'), 30 | value = $target.text().trim(), 31 | $inp = $target.find('input'), 32 | idx, 33 | $row = $("td:contains('" + value + "')", $grid) 34 | .closest("tr"); 35 | if ((idx = options.indexOf(id)) > -1) { 36 | options.splice(idx, 1); 37 | $row.show(); 38 | setTimeout(function () { 39 | $inp.prop('checked', true); 40 | updateSelectAll(self); 41 | }, 0); 42 | } else { 43 | options.push(id); 44 | $row.hide(); 45 | setTimeout(function () { 46 | $inp.prop('checked', false); 47 | updateSelectAll(self); 48 | }, 0); 49 | } 50 | $(event.target).blur(); 51 | return false; 52 | }); 53 | }); -------------------------------------------------------------------------------- /WebGridExample/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /WebGridExample/Helpers/Html/EditableHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web.Mvc; 3 | using WebGridExample.Models; 4 | 5 | namespace WebGridExample.Helpers.Html 6 | { 7 | public static class EditableHelpers 8 | { 9 | public static MvcHtmlString DisplayRecordOptions(this HtmlHelper helper, User user) 10 | { 11 | // Text Display 12 | var toolbar = new TagBuilder("ul"); 13 | toolbar.AddCssClass("record-toolbar"); 14 | 15 | var result = String.Concat( 16 | GetEditButton().ToString(TagRenderMode.Normal), 17 | GetSaveButton().ToString(TagRenderMode.Normal), 18 | GetCancelButton().ToString(TagRenderMode.Normal) 19 | ); 20 | 21 | toolbar.InnerHtml = result; 22 | 23 | return MvcHtmlString.Create(toolbar.ToString(TagRenderMode.Normal)); 24 | } 25 | 26 | private static TagBuilder GetEditButton() 27 | { 28 | var editButton = new TagBuilder("li") 29 | { 30 | InnerHtml = GetIcon("edit") 31 | }; 32 | 33 | editButton.AddCssClass("edit-button btn btn-default btn-sm"); 34 | editButton.Attributes.Add("title", "Edit Record"); 35 | 36 | return editButton; 37 | } 38 | private static TagBuilder GetCancelButton() 39 | { 40 | var cancelButton = new TagBuilder("li") 41 | { 42 | InnerHtml = GetIcon("ban-circle") 43 | }; 44 | 45 | cancelButton.AddCssClass("cancel-button hide btn btn-default btn-sm"); 46 | cancelButton.Attributes.Add("title", "Cancel Editing"); 47 | 48 | return cancelButton; 49 | } 50 | private static TagBuilder GetSaveButton() 51 | { 52 | var saveButton = new TagBuilder("li") 53 | { 54 | InnerHtml = GetIcon("floppy-disk") 55 | }; 56 | 57 | saveButton.AddCssClass("save-button hide btn btn-default btn-sm"); 58 | saveButton.Attributes.Add("title", "Save Record"); 59 | 60 | return saveButton; 61 | } 62 | private static string GetIcon(string iconName) 63 | { 64 | var icon = new TagBuilder("i"); 65 | icon.AddCssClass(String.Format("glyphicon glyphicon-{0}", iconName)); 66 | 67 | return icon.ToString(TagRenderMode.Normal); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /WebGridExample/ActionResults/BatchCommandResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Web.Mvc; 4 | using WebGridExample.Interface; 5 | 6 | namespace WebGridExample.ActionResults 7 | { 8 | public class BatchCommandResult : CommandResult 9 | { 10 | public BatchCommandResult(ControllerContext context, Func successResult) 11 | : base(context, successResult) 12 | { 13 | // What is the name of the checkbox in the Grid? 14 | BatchName = "select"; 15 | } 16 | public override void ExecuteResult(ControllerContext context) 17 | { 18 | ExecuteCommands(); 19 | var command = ExecuteNonCommands(); // i.e. paging, etc. 20 | SuccessResult(command).ExecuteResult(context); 21 | } 22 | private IFormCommand ExecuteNonCommands() 23 | { 24 | foreach (var command in FormCollection) 25 | { 26 | var formCommand = Commands.FirstOrDefault(e => e.CommandName == command.ToString()); 27 | if (formCommand == null) continue; 28 | 29 | var value = FormCollection[formCommand.CommandName]; 30 | if (String.IsNullOrEmpty(value)) continue; 31 | 32 | var idList = value.Split(','); 33 | foreach (var valueList in idList) 34 | { 35 | formCommand.Execute(valueList); 36 | } 37 | return formCommand; 38 | } 39 | return null; 40 | } 41 | private void ExecuteCommands() 42 | { 43 | if (!IsSelected) return; 44 | var idList = SelectionItem.Split(','); 45 | var command = GetKnownCommand(); 46 | if (command != null) 47 | { 48 | foreach (var valueList in idList) 49 | { 50 | command.Execute(valueList); 51 | } 52 | } 53 | } 54 | protected bool IsSelected 55 | { 56 | get 57 | { 58 | var result = false; 59 | if (FormCollection != null && FormCollection.Count > 0) 60 | { 61 | result = !String.IsNullOrEmpty(FormCollection[BatchName]); 62 | } 63 | return result; 64 | } 65 | } 66 | protected string SelectionItem => FormCollection[BatchName]; 67 | } 68 | } -------------------------------------------------------------------------------- /WebGridExample/Helpers/Html/WebGridHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | using System.Web.Mvc.Html; 7 | using WebGridExample.Models; 8 | 9 | namespace WebGridExample.Helpers.Html 10 | { 11 | public static class WebGridHelpers 12 | { 13 | public static HtmlString WebGridFilter(this HtmlHelper helper, 14 | IEnumerable users, Func property, 15 | string headingText) where T : class 16 | { 17 | var model = new WebGridFilterModel 18 | { 19 | Users = users.GroupBy(property).Select(g => g.First()), 20 | Property = property, 21 | HeadingText = headingText 22 | }; 23 | return helper.Partial("_webGridFilter", model); 24 | } 25 | 26 | public static MvcHtmlString EditableTextBox(this HtmlHelper helper, 27 | string value, User user, string name) 28 | { 29 | // Text Display 30 | var span = new TagBuilder("span") { InnerHtml = value }; 31 | span.AddCssClass("cell-value"); 32 | 33 | // Input display. 34 | var formatName = HtmlHelper.GenerateIdFromName(name); 35 | 36 | var uniqueId = String.Format("{0}_{1}", formatName, user.Id); 37 | 38 | var input = helper.TextBox(uniqueId, 39 | value, new { @class = "hide input-sm" }); 40 | 41 | var result = String.Concat( 42 | span.ToString(TagRenderMode.Normal), 43 | input.ToHtmlString() 44 | ); 45 | 46 | return MvcHtmlString.Create(result); 47 | } 48 | 49 | public static MvcHtmlString EditableDateTime(this HtmlHelper helper, 50 | DateTime value, User user, string name) 51 | { 52 | // Text Display 53 | var span = new TagBuilder("span") { InnerHtml = value.ToString("yyyy-MM-dd") }; 54 | span.AddCssClass("cell-value"); 55 | 56 | // Input display. 57 | var formatName = HtmlHelper.GenerateIdFromName(name); 58 | 59 | var uniqueId = String.Format("{0}_{1}", formatName, user.Id); 60 | 61 | var input = helper.TextBox(uniqueId, value.ToString("yyyy-MM-dd"), 62 | new { @type = "date", @class = "hide input-sm" }); 63 | 64 | var result = String.Concat( 65 | span.ToString(TagRenderMode.Normal), 66 | input.ToHtmlString() 67 | ); 68 | return MvcHtmlString.Create(result); 69 | } 70 | 71 | } 72 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | *.cs linguist-language=CSharp 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /WebGridExample/Formatters/ExcelFormatter.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Text; 3 | using WebGridExample.Models; 4 | 5 | namespace WebGridExample.Formatters 6 | { 7 | public class ExcelFormatter 8 | { 9 | private readonly IQueryable _records; 10 | 11 | public ExcelFormatter(IQueryable records) 12 | { 13 | _records = records; 14 | } 15 | 16 | public byte[] CreateXmlWorksheet() 17 | { 18 | var xmlTemplate = WebGridResources.ExcelXmlTemplate; 19 | var styles = GetStyles(); 20 | var header = WriteHeader(); 21 | var xmlData = GetRecords(); 22 | 23 | var excelXml = string.Format("{0}{1}", header, xmlData); 24 | xmlTemplate = xmlTemplate.Replace("$ROWSPLACEHOLDER$", excelXml); 25 | xmlTemplate = xmlTemplate.Replace("$STYLEPLACEHOLDER$", styles); 26 | 27 | return Encoding.UTF8.GetBytes(xmlTemplate); 28 | } 29 | 30 | private string GetStyles() 31 | { 32 | return @""; 34 | 35 | } 36 | 37 | private string GetRecords() 38 | { 39 | var sb = new StringBuilder(); 40 | foreach (var record in _records) 41 | { 42 | sb.Append(""); 43 | sb.Append("" + record.Id + ""); 44 | sb.Append("" + record.UserName + ""); 45 | sb.Append("" + record.FirstName + ""); 46 | sb.Append("" + record.LastName + ""); 47 | sb.Append("" + 48 | record.LastLogin.ToOADate() + ""); 49 | sb.Append(""); 50 | } 51 | 52 | return sb.ToString(); 53 | } 54 | 55 | private string WriteHeader() 56 | { 57 | var header = new StringBuilder(); 58 | header.Append(""); 59 | header.Append("Id"); 60 | header.Append("User Name"); 61 | header.Append("First Name"); 62 | header.Append("Last Name"); 63 | header.Append("Last Login"); 64 | header.Append(""); 65 | 66 | return header.ToString(); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /WebGridExample/Helpers/Html/PagingExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Web.Mvc; 5 | using MvcPaging; 6 | 7 | namespace WebGridExample.Helpers.Html 8 | { 9 | public static class PagingExtensions 10 | { 11 | #region Set PageSize 12 | 13 | public static MvcHtmlString PageSizeSelector(this HtmlHelper helper, 14 | IPagedList list) where T : class 15 | { 16 | var div = new TagBuilder("div"); 17 | div.AddCssClass("btn-group"); 18 | 19 | TagBuilder rowSelect = GetRowSelect(list); 20 | div.InnerHtml = String.Format("{0}", 21 | rowSelect.ToString(TagRenderMode.Normal)); 22 | 23 | return new MvcHtmlString(div.ToString(TagRenderMode.Normal)); 24 | } 25 | 26 | private static TagBuilder GetRowSelect(IPagedList list) 27 | { 28 | var rowSelect = new TagBuilder("select"); 29 | rowSelect.Attributes.Add("id", "size"); 30 | rowSelect.Attributes.Add("name", "size"); 31 | rowSelect.Attributes.Add("class", "size"); 32 | // Define the amount of rows to return. 33 | var rows = new Dictionary 34 | { 35 | {"10", "10"}, 36 | {"25", "25"}, 37 | {"50", "50"}, 38 | {"100", "100"}, 39 | {"500", "500"} 40 | }; 41 | 42 | var rowBuilder = new StringBuilder(); 43 | foreach (var row in rows) 44 | { 45 | int count; 46 | if (!int.TryParse(row.Value, out count)) 47 | { 48 | count = 0; 49 | } 50 | rowBuilder.AppendFormat( 51 | count == list.PageSize 52 | ? "" 53 | : "", row.Value, row.Key); 54 | } 55 | rowSelect.InnerHtml = rowBuilder.ToString(); 56 | 57 | return rowSelect; 58 | } 59 | 60 | #endregion 61 | 62 | #region Display Record Range 63 | 64 | public static MvcHtmlString PageRangeDisplay(this HtmlHelper helper, IPagedList list) where T : class 65 | { 66 | return MvcHtmlString.Create(GetRecordTotal(list).ToHtmlString()); 67 | } 68 | 69 | private static MvcHtmlString GetRecordTotal(IPagedList list) 70 | { 71 | var recordTotal = new TagBuilder("label"); 72 | 73 | var start = list.PageIndex * list.PageSize + 1; 74 | var end = start + list.PageSize - 1; 75 | if (end > list.TotalItemCount) 76 | { 77 | end = list.TotalItemCount; 78 | } 79 | recordTotal.InnerHtml = String.Format("{0}-{1} of {2}", start, end, list.TotalItemCount); 80 | 81 | return MvcHtmlString.Create(recordTotal.ToString(TagRenderMode.Normal)); 82 | } 83 | 84 | #endregion 85 | 86 | } 87 | } -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridBatch.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.WebGridBatchViewModel 3 | @{ 4 | var grid = new WebGrid(Model.Users, canPage: false); 5 | } 6 | 7 | 8 | 9 | WebGrid Example 10 | 11 | 15 | 16 | 17 | 18 |

WebGrid Example

19 | 20 | @if (Model != null) 21 | { 22 |
23 | 24 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 25 | { 26 | 40 | 41 | @MvcHtmlString.Create( 42 | grid.GetHtml( 43 | htmlAttributes: new 44 | { 45 | id = "grid", 46 | @class = "table table-bordered table-striped table-condensed" 47 | }, 48 | emptyRowCellValue: "No Records Found", 49 | headerStyle: "grid-header", 50 | columns: grid.Columns( 51 | grid.Column(header: "{CheckBoxHeading}", 52 | format: @, 53 | style: "text-center checkbox-width"), 54 | grid.Column("UserName", "User Name", @@item.Value.UserName), 55 | grid.Column("FirstName", "First Name", @@item.Value.FirstName), 56 | grid.Column("LastName", "Last Name", @@item.Value.LastName), 57 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) 58 | ) 59 | ) 60 | .ToString() 61 | .Replace("{CheckBoxHeading}", "
") 62 | ) 63 | } 64 |
65 | 66 | } 67 | 68 | 69 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/Index.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.UserViewModel 3 | 4 | 5 | 6 | WebGrid Example 7 | 8 | 17 | 18 | 19 | 20 |

WebGrid Example

21 | 22 | @if (Model != null) 23 | { 24 |
25 | @Html.Partial("userGrid", Model.Users) 26 |
27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /WebGridExample/Repository/Repository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using System.Data.Entity; 5 | using System.Data.Entity.Core.Objects; 6 | using System.Data.Entity.Infrastructure; 7 | using System.Data.SqlClient; 8 | using System.Linq; 9 | using System.Linq.Expressions; 10 | using WebGridExample.Interface; 11 | 12 | namespace WebGridExample.Repository 13 | { 14 | public class Repository : IRepository where TEntity : class 15 | { 16 | protected DbContext DbContext; 17 | protected ObjectSet ObjectSet; 18 | 19 | public Repository(DbContext context) 20 | { 21 | DbContext = context; 22 | ObjectSet = 23 | ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet(); 24 | } 25 | 26 | public virtual IQueryable GetAll() 27 | { 28 | return DbContext.Set(); 29 | } 30 | 31 | public virtual IQueryable Find(Expression> predicate) 32 | { 33 | return DbContext.Set().AsNoTracking().Where(predicate); 34 | } 35 | 36 | public virtual int Count(Expression> predicate) 37 | { 38 | return DbContext.Set().Count(predicate); 39 | } 40 | 41 | public int Add(TEntity entity) 42 | { 43 | if (entity == null) 44 | throw new ArgumentNullException("entity"); 45 | 46 | DbContext.Set().Add(entity); 47 | return DbContext.SaveChanges(); 48 | } 49 | 50 | public int SaveChanges() 51 | { 52 | return DbContext.SaveChanges(); 53 | } 54 | 55 | public int Delete(TEntity entity) 56 | { 57 | if (entity == null) 58 | throw new ArgumentNullException("Entity Issue. It''s null."); 59 | 60 | DbContext.Entry(entity).State = EntityState.Deleted; 61 | return DbContext.SaveChanges(); 62 | } 63 | 64 | public TEntity First(Expression> predicate) 65 | { 66 | return DbContext.Set().FirstOrDefault(predicate); 67 | } 68 | 69 | protected virtual T ExecuteReader(Func mapEntities, 70 | string exec, params object[] parameters) 71 | { 72 | using (var conn = new SqlConnection(DbContext.Database.Connection.ConnectionString)) 73 | { 74 | using (var command = new SqlCommand(exec, conn)) 75 | { 76 | conn.Open(); 77 | command.Parameters.AddRange(parameters); 78 | command.CommandType = CommandType.StoredProcedure; 79 | try 80 | { 81 | using (var reader = command.ExecuteReader()) 82 | { 83 | T data = mapEntities(reader); 84 | return data; 85 | } 86 | } 87 | finally 88 | { 89 | conn.Close(); 90 | } 91 | 92 | } 93 | } 94 | } 95 | 96 | public void Dispose() 97 | { 98 | Dispose(true); 99 | GC.SuppressFinalize(this); 100 | } 101 | 102 | protected virtual void Dispose(bool disposing) 103 | { 104 | if (!disposing) return; 105 | 106 | if (DbContext == null) return; 107 | 108 | DbContext.Dispose(); 109 | DbContext = null; 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | 60 | # Chutzpah Test files 61 | _Chutzpah* 62 | 63 | # Visual C++ cache files 64 | ipch/ 65 | *.aps 66 | *.ncb 67 | *.opensdf 68 | *.sdf 69 | *.cachefile 70 | 71 | # Visual Studio profiler 72 | *.psess 73 | *.vsp 74 | *.vspx 75 | 76 | # TFS 2012 Local Workspace 77 | $tf/ 78 | 79 | # Guidance Automation Toolkit 80 | *.gpState 81 | 82 | # ReSharper is a .NET coding add-in 83 | _ReSharper*/ 84 | *.[Rr]e[Ss]harper 85 | *.DotSettings.user 86 | 87 | # JustCode is a .NET coding addin-in 88 | .JustCode 89 | 90 | # TeamCity is a build add-in 91 | _TeamCity* 92 | 93 | # DotCover is a Code Coverage Tool 94 | *.dotCover 95 | 96 | # NCrunch 97 | _NCrunch_* 98 | .*crunch*.local.xml 99 | 100 | # MightyMoose 101 | *.mm.* 102 | AutoTest.Net/ 103 | 104 | # Web workbench (sass) 105 | .sass-cache/ 106 | 107 | # Installshield output folder 108 | [Ee]xpress/ 109 | 110 | # DocProject is a documentation generator add-in 111 | DocProject/buildhelp/ 112 | DocProject/Help/*.HxT 113 | DocProject/Help/*.HxC 114 | DocProject/Help/*.hhc 115 | DocProject/Help/*.hhk 116 | DocProject/Help/*.hhp 117 | DocProject/Help/Html2 118 | DocProject/Help/html 119 | 120 | # Click-Once directory 121 | publish/ 122 | 123 | # Publish Web Output 124 | *.[Pp]ublish.xml 125 | *.azurePubxml 126 | ## TODO: Comment the next line if you want to checkin your 127 | ## web deploy settings but do note that will include unencrypted 128 | ## passwords 129 | #*.pubxml 130 | 131 | # NuGet Packages Directory 132 | packages/* 133 | ## TODO: If the tool you use requires repositories.config 134 | ## uncomment the next line 135 | #!packages/repositories.config 136 | 137 | # Enable "build/" folder in the NuGet Packages folder since 138 | # NuGet packages use it for MSBuild targets. 139 | # This line needs to be after the ignore of the build folder 140 | # (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file 167 | # to a newer Visual Studio version. Backup files are not needed, 168 | # because we have git ;-) 169 | _UpgradeReport_Files/ 170 | Backup*/ 171 | UpgradeLog*.XML 172 | UpgradeLog*.htm 173 | 174 | # SQL Server files 175 | *.mdf 176 | *.ldf 177 | 178 | # Business Intelligence projects 179 | *.rdl.data 180 | *.bim.layout 181 | *.bim_*.settings 182 | 183 | # Microsoft Fakes 184 | FakesAssemblies/ 185 | 186 | # LightSwitch generated files 187 | GeneratedArtifacts/ 188 | _Pvt_Extensions/ 189 | ModelManifest.xml 190 | /NDependOut 191 | /.vs/WebGridExample/v15/Server/sqlite3/db.lock 192 | /.vs/WebGridExample/v15/Server/sqlite3 193 | -------------------------------------------------------------------------------- /WebGridExample/Web.config: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /WebGridExample/WebGridResources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34209 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace WebGridExample { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | public class WebGridResources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal WebGridResources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | public static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WebGridExample.WebGridResources", typeof(WebGridResources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | public static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// Looks up a localized string similar to <?xml version="1.0" encoding="utf-8" ?> 65 | ///<?mso-application progid="Excel.Sheet" ?> 66 | ///<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 67 | /// xmlns:o="urn:schemas-microsoft-com:office:office" 68 | /// xmlns:x="urn:schemas-microsoft-com:office:excel" 69 | /// xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 70 | /// xmlns:html="http://www.w3.org/TR/REC-html40"> 71 | /// $STYLEPLACEHOLDER$ 72 | /// <Worksheet ss:Name="Sheet1"> 73 | /// <Table> 74 | /// $ROWSPLACEHOLDER$ 75 | /// </Table> 76 | /// </Works [rest of string was truncated]";. 77 | /// 78 | public static string ExcelXmlTemplate { 79 | get { 80 | return ResourceManager.GetString("ExcelXmlTemplate", resourceCulture); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /WebGridExample/Controllers/UserController.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Web.Mvc; 3 | using WebGridExample.ActionResults; 4 | using WebGridExample.Formatters; 5 | using WebGridExample.Interface; 6 | using WebGridExample.Repository; 7 | using WebGridExample.ViewModel; 8 | 9 | namespace WebGridExample.Controllers 10 | { 11 | public class UserController : Controller 12 | { 13 | private readonly IUserRepository _repository; 14 | 15 | public UserController() : this(new UserRepository()) { } 16 | public UserController(IUserRepository repository) 17 | { 18 | _repository = repository; 19 | } 20 | 21 | #region WebGrid Batch 22 | 23 | // GET: User 24 | public ActionResult WebGridBatch() 25 | { 26 | var model = new WebGridBatchViewModel 27 | { 28 | Users = _repository.GetAll() 29 | }; 30 | return View(model); 31 | } 32 | 33 | [HttpPost] 34 | public ActionResult WebGridBatch(WebGridBatchViewModel model) 35 | { 36 | if (model.Delete) 37 | { 38 | foreach (var user in model.SelectedUsers) 39 | { 40 | var loadedUser = _repository.GetById(user.Id); 41 | _repository.Delete(loadedUser); 42 | _repository.SaveChanges(); 43 | } 44 | } 45 | 46 | return Redirect(Url.Content("~/")); 47 | } 48 | 49 | 50 | #endregion 51 | 52 | #region WebGrid Excel 53 | 54 | // GET: User 55 | public ActionResult WebGridExcel() 56 | { 57 | var model = new WebGridExcelViewModel 58 | { 59 | Users = _repository.GetAll() 60 | }; 61 | return View(model); 62 | } 63 | 64 | #endregion 65 | 66 | #region WebGrid CSV 67 | 68 | // GET: User 69 | public ActionResult WebGridCSV() 70 | { 71 | var model = new WebGridExcelViewModel 72 | { 73 | Users = _repository.GetAll() 74 | }; 75 | return View(model); 76 | } 77 | 78 | #endregion 79 | 80 | #region WebGrid Lazy-Loading webAPI 81 | 82 | public ActionResult WebGridLLWebApi(int? page) 83 | { 84 | var defaultPageSize = 5; 85 | var model = new WebGridWebApiViewModel 86 | { 87 | Users = _repository.GetPagedUsers(page, defaultPageSize) 88 | }; 89 | return View(model); 90 | } 91 | 92 | #endregion 93 | 94 | #region WebGrid Lazy-Loading SignalR 95 | 96 | public ActionResult WebGridLLSignalR(int? page) 97 | { 98 | var defaultPageSize = 5; 99 | var model = new WebGridWebApiViewModel 100 | { 101 | Users = _repository.GetPagedUsers(page, defaultPageSize) 102 | }; 103 | return View(model); 104 | } 105 | 106 | #endregion 107 | 108 | #region WebGrid Inline Editing 109 | 110 | public ActionResult WebGridInlineEditing(int? page) 111 | { 112 | var defaultPageSize = 5; 113 | var model = new WebGridWebApiViewModel 114 | { 115 | Users = _repository.GetPagedUsers(page, defaultPageSize) 116 | }; 117 | return View(model); 118 | } 119 | 120 | #endregion 121 | 122 | #region WebGrid Validating 123 | 124 | public ActionResult WebGridValidating(int? page) 125 | { 126 | var defaultPageSize = 5; 127 | var model = new WebGridWebApiViewModel 128 | { 129 | Users = _repository.GetPagedUsers(page, defaultPageSize) 130 | }; 131 | return View(model); 132 | } 133 | 134 | #endregion 135 | 136 | #region WebGrid Excel Filtering 137 | 138 | public ActionResult WebGridExcelFiltering() 139 | { 140 | var model = new WebGridBatchViewModel 141 | { 142 | Users = _repository.GetAll() 143 | }; 144 | return View(model); 145 | } 146 | 147 | 148 | #endregion 149 | 150 | [HttpPost] 151 | public ActionResult Export(ExportParameters model) 152 | { 153 | var records = _repository.GetAll(); 154 | if (model.PagingEnabled) 155 | { 156 | records = records.Skip((model.CurrentPage - 1) * model.PageSize) 157 | .Take(model.PageSize); 158 | } 159 | if (model.OutputType.Equals(Output.Excel)) 160 | { 161 | var excelFormatter = new ExcelFormatter(records); 162 | return new ExcelResult(excelFormatter.CreateXmlWorksheet(), "Sample.xlsx"); 163 | } 164 | 165 | if (model.OutputType.Equals(Output.Csv)) 166 | { 167 | return new CsvResult(records, "Sample.csv"); 168 | } 169 | 170 | return Redirect(Url.Content("~/")); 171 | } 172 | } 173 | } -------------------------------------------------------------------------------- /WebGridExample/Views/Shared/UserGrid.cshtml: -------------------------------------------------------------------------------- 1 | @using WebGridExample.Helpers.Html 2 | @using WebGridExample.Models 3 | @model IEnumerable 4 | @{ 5 | var grid = new WebGrid(Model, canPage: false); 6 | } 7 | 8 | @using (Html.BeginForm("Index", "User", FormMethod.Post, new {@role = "search"})) 9 | { 10 | 30 | 31 | @MvcHtmlString.Create( 32 | grid.GetHtml( 33 | htmlAttributes: new 34 | { 35 | id = "grid", 36 | @class = "table table-bordered table-striped table-condensed" 37 | }, 38 | emptyRowCellValue: "No Records Found", 39 | headerStyle: "grid-header", 40 | columns: grid.Columns( 41 | grid.Column(header: "{CheckBoxHeading}", 42 | format: item => Html.CheckBox("select", new { @class = "box", @value = item.Value.Id }), 43 | style: "text-center checkbox-width"), 44 | grid.Column("UserName", "{UserName-filter}", @@item.Value.UserName, canSort: false), 45 | grid.Column("FirstName", "First Name", @@item.Value.FirstName, canSort: false), 46 | grid.Column("LastName", "Last Name", @@item.Value.LastName, canSort: false), 47 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString(), canSort: false) 48 | ) 49 | ) 50 | .ToString() 51 | .Replace("{CheckBoxHeading}", "
") 52 | .Replace("{UserName-filter}", Html.WebGridFilter(Model, e=> e.UserName, "User Name").ToString()) 53 | ) 54 | } 55 | 56 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 57 | { 58 | 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /WebGridExample/Resources/CommonResources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace WebGridExample.Resources { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class CommonResources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal CommonResources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WebGridExample.Resources.CommonResources", typeof(CommonResources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// Looks up a localized string similar to Value cannot be null or an empty string.. 65 | /// 66 | internal static string Argument_Cannot_Be_Null_Or_Empty { 67 | get { 68 | return ResourceManager.GetString("Argument_Cannot_Be_Null_Or_Empty", resourceCulture); 69 | } 70 | } 71 | 72 | /// 73 | /// Looks up a localized string similar to Value must be between {0} and {1}.. 74 | /// 75 | internal static string Argument_Must_Be_Between { 76 | get { 77 | return ResourceManager.GetString("Argument_Must_Be_Between", resourceCulture); 78 | } 79 | } 80 | 81 | /// 82 | /// Looks up a localized string similar to Value must be a value from the "{0}" enumeration.. 83 | /// 84 | internal static string Argument_Must_Be_Enum_Member { 85 | get { 86 | return ResourceManager.GetString("Argument_Must_Be_Enum_Member", resourceCulture); 87 | } 88 | } 89 | 90 | /// 91 | /// Looks up a localized string similar to Value must be greater than {0}.. 92 | /// 93 | internal static string Argument_Must_Be_GreaterThan { 94 | get { 95 | return ResourceManager.GetString("Argument_Must_Be_GreaterThan", resourceCulture); 96 | } 97 | } 98 | 99 | /// 100 | /// Looks up a localized string similar to Value must be greater than or equal to {0}.. 101 | /// 102 | internal static string Argument_Must_Be_GreaterThanOrEqualTo { 103 | get { 104 | return ResourceManager.GetString("Argument_Must_Be_GreaterThanOrEqualTo", resourceCulture); 105 | } 106 | } 107 | 108 | /// 109 | /// Looks up a localized string similar to Value must be less than {0}.. 110 | /// 111 | internal static string Argument_Must_Be_LessThan { 112 | get { 113 | return ResourceManager.GetString("Argument_Must_Be_LessThan", resourceCulture); 114 | } 115 | } 116 | 117 | /// 118 | /// Looks up a localized string similar to Value must be less than or equal to {0}.. 119 | /// 120 | internal static string Argument_Must_Be_LessThanOrEqualTo { 121 | get { 122 | return ResourceManager.GetString("Argument_Must_Be_LessThanOrEqualTo", resourceCulture); 123 | } 124 | } 125 | 126 | /// 127 | /// Looks up a localized string similar to Value cannot be an empty string. It must either be null or a non-empty string.. 128 | /// 129 | internal static string Argument_Must_Be_Null_Or_Non_Empty { 130 | get { 131 | return ResourceManager.GetString("Argument_Must_Be_Null_Or_Non_Empty", resourceCulture); 132 | } 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /WebGridExample/WebGridResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | <?xml version="1.0" encoding="utf-8" ?> 122 | <?mso-application progid="Excel.Sheet" ?> 123 | <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 124 | xmlns:o="urn:schemas-microsoft-com:office:office" 125 | xmlns:x="urn:schemas-microsoft-com:office:excel" 126 | xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 127 | xmlns:html="http://www.w3.org/TR/REC-html40"> 128 | $STYLEPLACEHOLDER$ 129 | <Worksheet ss:Name="Sheet1"> 130 | <Table> 131 | $ROWSPLACEHOLDER$ 132 | </Table> 133 | </Worksheet> 134 | </Workbook> 135 | 136 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridExcel.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.WebGridBatchViewModel 3 | @{ 4 | var grid = new WebGrid(Model.Users, canPage: false); 5 | } 6 | 7 | 8 | 9 | WebGrid Example 10 | 11 | 20 | 21 | 22 | 23 |

WebGrid Example

24 | 25 | @if (Model != null) 26 | { 27 |
28 | 29 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 30 | { 31 | 51 | 52 | @MvcHtmlString.Create( 53 | grid.GetHtml( 54 | htmlAttributes: new 55 | { 56 | id = "grid", 57 | @class = "table table-bordered table-striped table-condensed" 58 | }, 59 | emptyRowCellValue: "No Records Found", 60 | headerStyle: "grid-header", 61 | columns: grid.Columns( 62 | grid.Column(header: "{CheckBoxHeading}", 63 | format: @, 64 | style: "text-center checkbox-width"), 65 | grid.Column("UserName", "User Name", @@item.Value.UserName), 66 | grid.Column("FirstName", "First Name", @@item.Value.FirstName), 67 | grid.Column("LastName", "Last Name", @@item.Value.LastName), 68 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) 69 | ) 70 | ) 71 | .ToString() 72 | .Replace("{CheckBoxHeading}", "
") 73 | ) 74 | } 75 |
76 | 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 92 | 93 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 94 | { 95 | 144 | } 145 | 146 | 147 | -------------------------------------------------------------------------------- /WebGridExample/Resources/CommonResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Value cannot be null or an empty string. 122 | 123 | 124 | Value must be between {0} and {1}. 125 | 126 | 127 | Value must be a value from the "{0}" enumeration. 128 | 129 | 130 | Value must be greater than {0}. 131 | 132 | 133 | Value must be greater than or equal to {0}. 134 | 135 | 136 | Value must be less than {0}. 137 | 138 | 139 | Value must be less than or equal to {0}. 140 | 141 | 142 | Value cannot be an empty string. It must either be null or a non-empty string. 143 | 144 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridCSV.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.WebGridBatchViewModel 3 | @{ 4 | var grid = new WebGrid(Model.Users, canPage: false); 5 | } 6 | 7 | 8 | 9 | WebGrid Example 10 | 11 | 20 | 21 | 22 | 23 |

WebGrid Example

24 | 25 | @if (Model != null) 26 | { 27 |
28 | 29 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 30 | { 31 | 51 | 52 | @MvcHtmlString.Create( 53 | grid.GetHtml( 54 | htmlAttributes: new 55 | { 56 | id = "grid", 57 | @class = "table table-bordered table-striped table-condensed" 58 | }, 59 | emptyRowCellValue: "No Records Found", 60 | headerStyle: "grid-header", 61 | columns: grid.Columns( 62 | grid.Column(header: "{CheckBoxHeading}", 63 | format: @, 64 | style: "text-center checkbox-width"), 65 | grid.Column("UserName", "User Name", @@item.Value.UserName), 66 | grid.Column("FirstName", "First Name", @@item.Value.FirstName), 67 | grid.Column("LastName", "Last Name", @@item.Value.LastName), 68 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) 69 | ) 70 | ) 71 | .ToString() 72 | .Replace("{CheckBoxHeading}", "
") 73 | ) 74 | } 75 |
76 | 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 92 | 93 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 94 | { 95 | 150 | } 151 | 152 | 153 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridExcelFiltering.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @using WebGridExample.Helpers.Html 3 | @using WebGridExample.Models 4 | @model WebGridExample.ViewModel.WebGridBatchViewModel 5 | @{ 6 | var grid = new WebGrid(Model.Users, canPage: false); 7 | } 8 | 9 | 10 | 11 | WebGrid Example 12 | 13 | 20 | 21 | 22 | 23 |

WebGrid Example

24 | 25 | @if (Model != null) 26 | { 27 |
28 | 29 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 30 | { 31 | 51 | 52 | @MvcHtmlString.Create( 53 | grid.GetHtml( 54 | htmlAttributes: new 55 | { 56 | id = "grid", 57 | @class = "table table-bordered table-striped table-condensed" 58 | }, 59 | emptyRowCellValue: "No Records Found", 60 | headerStyle: "grid-header", 61 | columns: grid.Columns( 62 | grid.Column(header: "{CheckBoxHeading}", 63 | format: item => Html.CheckBox("select", new { @class = "box", @value = item.Value.Id }), 64 | style: "text-center checkbox-width"), 65 | grid.Column("UserName", "{UserName-filter}", @@item.Value.UserName, canSort: false), 66 | grid.Column("FirstName", "First Name", @@item.Value.FirstName, canSort: false), 67 | grid.Column("LastName", "Last Name", @@item.Value.LastName, canSort: false), 68 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString(), canSort: false) 69 | ) 70 | ) 71 | .ToString() 72 | .Replace("{CheckBoxHeading}", "
") 73 | .Replace("{UserName-filter}", Html.WebGridFilter(Model.Users, e => e.UserName, "User Name").ToString()) 74 | ) 75 | } 76 |
77 | 78 | } 79 | 80 | 81 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 82 | { 83 | 138 | } 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridLLWebApi.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.WebGridWebApiViewModel 3 | @{ 4 | var grid = new WebGrid(Model.Users, canPage: false); 5 | } 6 | 7 | 8 | 9 | WebGrid Example 10 | 11 | 15 | 16 | 17 | 18 | 19 |

WebGrid Example

20 | 21 | @if (Model != null) 22 | { 23 |
24 | 25 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 26 | { 27 | 47 | 48 | @MvcHtmlString.Create( 49 | grid.GetHtml( 50 | htmlAttributes: new 51 | { 52 | id = "grid", 53 | @class = "table table-bordered table-striped table-condensed" 54 | }, 55 | emptyRowCellValue: "No Records Found", 56 | headerStyle: "grid-header", 57 | columns: grid.Columns( 58 | grid.Column(header: "{CheckBoxHeading}", 59 | format: @, 60 | style: "text-center checkbox-width"), 61 | grid.Column("UserName", "User Name", @@item.Value.UserName), 62 | grid.Column("FirstName", "First Name", @@item.Value.FirstName), 63 | grid.Column("LastName", "Last Name", @@item.Value.LastName), 64 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) 65 | ) 66 | ) 67 | .ToString() 68 | .Replace("{CheckBoxHeading}", "
") 69 | ) 70 | } 71 |
72 | 73 | } 74 | 75 |
76 | 77 | 78 | 79 | 80 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 81 | { 82 | 137 | } 138 | 139 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridLLSignalR.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @model WebGridExample.ViewModel.WebGridWebApiViewModel 3 | @{ 4 | var grid = new WebGrid(Model.Users, canPage: false); 5 | } 6 | 7 | 8 | 9 | WebGrid Example 10 | 11 | 15 | 16 | 17 | 18 | 19 |

WebGrid Example

20 | 21 | @if (Model != null) 22 | { 23 |
24 | 25 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 26 | { 27 | 47 | 48 | @MvcHtmlString.Create( 49 | grid.GetHtml( 50 | htmlAttributes: new 51 | { 52 | id = "grid", 53 | @class = "table table-bordered table-striped table-condensed" 54 | }, 55 | emptyRowCellValue: "No Records Found", 56 | headerStyle: "grid-header", 57 | columns: grid.Columns( 58 | grid.Column(header: "{CheckBoxHeading}", 59 | format: @, 60 | style: "text-center checkbox-width"), 61 | grid.Column("UserName", "User Name", @@item.Value.UserName), 62 | grid.Column("FirstName", "First Name", @@item.Value.FirstName), 63 | grid.Column("LastName", "Last Name", @@item.Value.LastName), 64 | grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) 65 | ) 66 | ) 67 | .ToString() 68 | .Replace("{CheckBoxHeading}", "
") 69 | ) 70 | } 71 |
72 | 73 | } 74 | 75 |
76 | 77 | 78 | 79 | 80 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 81 | { 82 | 137 | } 138 | 139 | 140 | 141 | 142 | 143 | 144 | 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridInlineEditing.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @using WebGridExample.Helpers.Html 3 | @using WebGridExample.Models 4 | @model WebGridExample.ViewModel.WebGridWebApiViewModel 5 | @{ 6 | var grid = new WebGrid(Model.Users, canPage: false); 7 | } 8 | 9 | 10 | 11 | WebGrid Example 12 | 13 | 20 | 21 | 22 | 23 | 24 |

WebGrid Example

25 | 26 | @if (Model != null) 27 | { 28 |
29 | 30 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 31 | { 32 | 52 | 53 | @MvcHtmlString.Create( 54 | grid.GetHtml( 55 | htmlAttributes: new 56 | { 57 | id = "grid", 58 | @class = "table table-bordered table-striped table-condensed" 59 | }, 60 | emptyRowCellValue: "No Records Found", 61 | headerStyle: "grid-header", 62 | columns: grid.Columns( 63 | grid.Column(header: "{CheckBoxHeading}", 64 | format: @, 65 | style: "text-center checkbox-width"), 66 | grid.Column("UserName", 67 | format: item => Html.EditableTextBox((item.Value as User).UserName, (item.Value as User), "UserName")), 68 | grid.Column("FirstName", 69 | format: item => Html.EditableTextBox((item.Value as User).FirstName, item.Value as User, "FirstName")), 70 | grid.Column("LastName", 71 | format: item => Html.EditableTextBox((item.Value as User).LastName, item.Value as User, "LastName")), 72 | grid.Column("LastLogin", 73 | format: item => Html.EditableDateTime((item.Value as User).LastLogin, item.Value as User, "LastLogin")), 74 | grid.Column("Options", 75 | format: item => Html.DisplayRecordOptions(item.Value as User), canSort: false) 76 | 77 | ) 78 | ) 79 | .ToString() 80 | .Replace("{CheckBoxHeading}", "
") 81 | ) 82 | } 83 |
84 | 85 | } 86 | 87 |
88 | 89 | 90 | 91 | 92 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 93 | { 94 | 149 | } 150 | 151 | 152 | 153 | 154 | 155 | 156 | 215 | 216 | 217 | 218 | -------------------------------------------------------------------------------- /WebGridExample/Views/User/WebGridValidating.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Web.Mvc.Html 2 | @using WebGridExample.Helpers.Html 3 | @using WebGridExample.Models 4 | @model WebGridExample.ViewModel.WebGridWebApiViewModel 5 | @{ 6 | var grid = new WebGrid(Model.Users, canPage: false); 7 | } 8 | 9 | 10 | 11 | WebGrid Example 12 | 13 | 20 | 21 | 22 | 23 | 24 |

WebGrid Example

25 | 26 | @if (Model != null) 27 | { 28 |
29 | 30 | @using (Html.BeginForm("WebGridBatch", "User", FormMethod.Post, new { @role = "search" })) 31 | { 32 | 52 | 53 | @MvcHtmlString.Create( 54 | grid.GetHtml( 55 | htmlAttributes: new 56 | { 57 | id = "grid", 58 | @class = "table table-bordered table-striped table-condensed" 59 | }, 60 | emptyRowCellValue: "No Records Found", 61 | headerStyle: "grid-header", 62 | columns: grid.Columns( 63 | grid.Column(header: "{CheckBoxHeading}", 64 | format: @, 65 | style: "text-center checkbox-width"), 66 | grid.Column("UserName", 67 | format: item => Html.EditableTextBox((item.Value as User).UserName, (item.Value as User), "UserName")), 68 | grid.Column("FirstName", 69 | format: item => Html.EditableTextBox((item.Value as User).FirstName, item.Value as User, "FirstName")), 70 | grid.Column("LastName", 71 | format: item => Html.EditableTextBox((item.Value as User).LastName, item.Value as User, "LastName")), 72 | grid.Column("LastLogin", 73 | format: item => Html.EditableDateTime((item.Value as User).LastLogin, item.Value as User, "LastLogin")), 74 | grid.Column("Options", 75 | format: item => Html.DisplayRecordOptions(item.Value as User), canSort: false) 76 | 77 | ) 78 | ) 79 | .ToString() 80 | .Replace("{CheckBoxHeading}", "
") 81 | ) 82 | } 83 |
84 | 85 | } 86 | 87 |
88 | 89 | 90 | 91 | 92 | @using (Html.BeginForm("Export", "User", FormMethod.Post)) 93 | { 94 | 149 | } 150 | 151 | 152 | 153 | 154 | 155 | 156 | 232 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /WebGridExample/Resources/HelpersResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Argument conversion to type "{0}" failed. 122 | 123 | 124 | A series cannot be data-bound to a string object. 125 | 126 | 127 | The theme file "{0}" could not be found. 128 | 129 | 130 | The hash algorithm '{0}' is not supported, valid values are: sha256, sha1, md5 131 | 132 | 133 | "{0}" is invalid image format. Valid values are image format names like: "JPEG", "BMP", "GIF", "PNG", etc. 134 | 135 | 136 | Unable to convert to "{0}". Use Json.Decode<T> instead. 137 | 138 | 139 | Previously Displayed 140 | 141 | 142 | Accessing a property threw an exception: 143 | 144 | 145 | File path "{0}" is invalid. 146 | 147 | 148 | Additional server information is available when the page is running with high trust. 149 | 150 | 151 | Environment Variables 152 | 153 | 154 | ASP.NET Server Information 155 | 156 | 157 | HTTP Runtime Information 158 | 159 | 160 | Legacy Code Access Security 161 | 162 | 163 | Legacy Code Access Security has been detected on your system. Microsoft WebPage features require the ASP.NET 4 Code Access Security model. For information about how to resolve this, contact your server administrator. 164 | 165 | 166 | no value 167 | 168 | 169 | Server Configuration 170 | 171 | 172 | ASP.NET Server Variables 173 | 174 | 175 | The column name cannot be null or an empty string unless a custom format is specified. 176 | 177 | 178 | Column "{0}" does not exist. 179 | 180 | 181 | The WebGrid instance is already bound to a data source. 182 | 183 | 184 | A data source must be bound before this operation can be performed. 185 | 186 | 187 | This operation is not supported when paging is disabled for the "WebGrid" object. 188 | 189 | 190 | This operation is not supported when sorting is disabled for the "WebGrid" object. 191 | 192 | 193 | To use this argument, pager mode "{0}" must be enabled. 194 | 195 | 196 | This property cannot be set after the "WebGrid" object has been sorted or paged. Make sure that this property is set prior to invoking the "Rows" property directly or indirectly through other methods such as "GetHtml", "Pager", "Table", etc. 197 | 198 | 199 | A value for "rowCount" must be specified when "autoSortAndPage" is set to true and paging is enabled. 200 | 201 | 202 | Select 203 | 204 | 205 | The "fontColor" value is invalid. Valid values are names like "White", "Black", or "DarkBlue", or hexadecimal values in the form "#RRGGBB" or "#RGB". 206 | 207 | 208 | The "fontFamily" value is invalid. Valid values are font family names like: "Arial", "Times New Roman", etc. Make sure that the font family you are trying to use is installed on the server. 209 | 210 | 211 | The "fontStyle" value is invalid. Valid values are: "Regular", "Bold", "Italic", "Underline", and "Strikeout". 212 | 213 | 214 | The "horizontalAlign" value is invalid. Valid values are: "Right", "Left", and "Center". 215 | 216 | 217 | The "verticalAlign" value is invalid. Valid values are: "Top", "Bottom", and "Middle". 218 | 219 | 220 | Watermark width and height must both be positive or both be zero. 221 | 222 | 223 | An image could not be constructed from the content provided. 224 | 225 | 226 | The "priority" value is invalid. Valid values are "Low", "Normal" and "High". 227 | 228 | 229 | A string in the collection is null or empty. 230 | 231 | 232 | "SmtpServer" was not specified. 233 | 234 | 235 | No "From" email address was specified and a default value could not be assigned. 236 | 237 | -------------------------------------------------------------------------------- /WebGridExample/WebGridExample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8 | 9 | 2.0 10 | {65AE6C8D-B95F-4217-91A4-6D04B8CBC340} 11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} 12 | Library 13 | Properties 14 | WebGridExample 15 | WebGridExample 16 | v4.5 17 | true 18 | 19 | 20 | 21 | 22 | ..\ 23 | 24 | 25 | 26 | true 27 | full 28 | false 29 | bin\ 30 | DEBUG;TRACE 31 | prompt 32 | 4 33 | 34 | 35 | pdbonly 36 | true 37 | bin\ 38 | TRACE 39 | prompt 40 | 4 41 | 42 | 43 | 44 | ..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll 45 | True 46 | 47 | 48 | ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll 49 | True 50 | 51 | 52 | ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll 53 | True 54 | 55 | 56 | ..\packages\Microsoft.AspNet.SignalR.Core.2.2.0\lib\net45\Microsoft.AspNet.SignalR.Core.dll 57 | True 58 | 59 | 60 | ..\packages\Microsoft.AspNet.SignalR.SystemWeb.2.2.0\lib\net45\Microsoft.AspNet.SignalR.SystemWeb.dll 61 | True 62 | 63 | 64 | 65 | ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll 66 | True 67 | 68 | 69 | ..\packages\Microsoft.Owin.Host.SystemWeb.3.0.1\lib\net45\Microsoft.Owin.Host.SystemWeb.dll 70 | True 71 | 72 | 73 | ..\packages\Microsoft.Owin.Security.3.0.1\lib\net45\Microsoft.Owin.Security.dll 74 | True 75 | 76 | 77 | ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll 78 | True 79 | 80 | 81 | ..\packages\MvcPaging.2.1.4\lib\net40\MvcPaging.dll 82 | True 83 | 84 | 85 | ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll 86 | True 87 | 88 | 89 | ..\packages\Owin.1.0\lib\net40\Owin.dll 90 | True 91 | 92 | 93 | 94 | 95 | ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll 96 | True 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll 109 | True 110 | 111 | 112 | ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll 113 | True 114 | 115 | 116 | ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll 117 | True 118 | 119 | 120 | ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll 121 | True 122 | 123 | 124 | ..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll 125 | True 126 | 127 | 128 | ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll 129 | True 130 | 131 | 132 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll 133 | True 134 | 135 | 136 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll 137 | True 138 | 139 | 140 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll 141 | True 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | ..\packages\WebGrease.1.6.0\lib\WebGrease.dll 152 | True 153 | 154 | 155 | 156 | 157 | 158 | UserDatabase.mdf 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | Designer 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | ResXFileCodeGenerator 211 | CommonResources.Designer.cs 212 | Designer 213 | 214 | 215 | 216 | True 217 | True 218 | CommonResources.resx 219 | 220 | 221 | True 222 | True 223 | HelpersResources.resx 224 | 225 | 226 | 227 | 228 | 229 | 230 | Global.asax 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | True 252 | True 253 | WebGridResources.resx 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | Web.config 275 | 276 | 277 | Web.config 278 | 279 | 280 | 281 | 282 | ResXFileCodeGenerator 283 | HelpersResources.Designer.cs 284 | Designer 285 | 286 | 287 | PublicResXFileCodeGenerator 288 | WebGridResources.Designer.cs 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 10.0 297 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | True 307 | True 308 | 4537 309 | / 310 | http://localhost:4537/ 311 | False 312 | False 313 | 314 | 315 | False 316 | 317 | 318 | 319 | 320 | 327 | --------------------------------------------------------------------------------