diff --git a/WebGridExample/App_Data/UserDatabase.mdf b/WebGridExample/App_Data/UserDatabase.mdf index b1cb619..9c901ff 100644 Binary files a/WebGridExample/App_Data/UserDatabase.mdf and b/WebGridExample/App_Data/UserDatabase.mdf differ diff --git a/WebGridExample/App_Data/UserDatabase_log.ldf b/WebGridExample/App_Data/UserDatabase_log.ldf index 694fb7a..f239dc6 100644 Binary files a/WebGridExample/App_Data/UserDatabase_log.ldf and b/WebGridExample/App_Data/UserDatabase_log.ldf differ diff --git a/WebGridExample/Controllers/UserController.cs b/WebGridExample/Controllers/UserController.cs index 9a8d525..76aae32 100644 --- a/WebGridExample/Controllers/UserController.cs +++ b/WebGridExample/Controllers/UserController.cs @@ -5,11 +5,11 @@ namespace WebGridExample.Controllers { - public class UserController: Controller + public class UserController : Controller { private readonly IUserRepository _repository; - public UserController(): this(new UserRepository()) { } + public UserController() : this(new UserRepository()) { } public UserController(IUserRepository repository) { _repository = repository; @@ -24,5 +24,22 @@ public ActionResult Index() }; return View(model); } + + [HttpPost] + public ActionResult Index(UserViewModel model) + { + if (model.Delete) + { + foreach (var user in model.SelectedUsers) + { + var loadedUser = _repository.GetById(user.Id); + _repository.Delete(loadedUser); + _repository.SaveChanges(); + } + } + + return Redirect(Url.Content("~/")); + } + } } \ No newline at end of file diff --git a/WebGridExample/ModelBinders/UserViewModelBinder.cs b/WebGridExample/ModelBinders/UserViewModelBinder.cs new file mode 100644 index 0000000..4c9e56f --- /dev/null +++ b/WebGridExample/ModelBinders/UserViewModelBinder.cs @@ -0,0 +1,25 @@ +using System; +using System.Linq; +using System.Web.Mvc; +using WebGridExample.Models; +using WebGridExample.ViewModel; + +namespace WebGridExample.ModelBinders +{ + public class UserViewModelBinder : DefaultModelBinder + { + public override object BindModel(ControllerContext controllerContext, + ModelBindingContext bindingContext) + { + var request = controllerContext.HttpContext.Request; + var userIdList = request.Form.Get("select"); + var list = userIdList.Split(','); + + return new UserViewModel + { + SelectedUsers = list.Select(e=> new User{Id = Convert.ToInt32(e)}), + Delete = !String.IsNullOrEmpty(request.Form.Get("btnDelete")) + }; + } + } +} \ No newline at end of file diff --git a/WebGridExample/ViewModel/UserViewModel.cs b/WebGridExample/ViewModel/UserViewModel.cs index 8c295bd..bc59668 100644 --- a/WebGridExample/ViewModel/UserViewModel.cs +++ b/WebGridExample/ViewModel/UserViewModel.cs @@ -1,11 +1,17 @@ using System.Collections.Generic; +using System.Web.Mvc; +using WebGridExample.ModelBinders; using WebGridExample.Models; namespace WebGridExample.ViewModel { + [ModelBinder(typeof(UserViewModelBinder))] public class UserViewModel { public IEnumerable Users { get; set; } public User User { get; set; } + + public IEnumerable SelectedUsers { get; set; } + public bool Delete { get; set; } } } \ No newline at end of file diff --git a/WebGridExample/Views/Shared/UserGrid.cshtml b/WebGridExample/Views/Shared/UserGrid.cshtml new file mode 100644 index 0000000..cfcc406 --- /dev/null +++ b/WebGridExample/Views/Shared/UserGrid.cshtml @@ -0,0 +1,45 @@ +@model IEnumerable +@{ + var grid = new WebGrid(Model, canPage: false); +} + +@using (Html.BeginForm("Index", "User", FormMethod.Post, new {@role = "search"})) +{ + + + @MvcHtmlString.Create( + grid.GetHtml( + htmlAttributes: new + { + id = "grid", + @class = "table table-bordered table-striped table-condensed" + }, + emptyRowCellValue: "No Records Found", + headerStyle: "grid-header", + columns: grid.Columns( + grid.Column(header: "{CheckBoxHeading}", + format: @, + style: "text-center checkbox-width"), + grid.Column("UserName", "User Name", @@item.Value.UserName), + grid.Column("FirstName", "First Name", @@item.Value.FirstName), + grid.Column("LastName", "Last Name", @@item.Value.LastName), + grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) + ) + ) + .ToString() + .Replace("{CheckBoxHeading}", "
") + ) +} \ No newline at end of file diff --git a/WebGridExample/Views/User/Index.cshtml b/WebGridExample/Views/User/Index.cshtml index 85aeb32..a2124cd 100644 --- a/WebGridExample/Views/User/Index.cshtml +++ b/WebGridExample/Views/User/Index.cshtml @@ -1,39 +1,34 @@ -@model WebGridExample.ViewModel.UserViewModel +@using System.Web.Mvc.Html +@model WebGridExample.ViewModel.UserViewModel WebGrid Example + + + - @{ - var grid = new WebGrid(Model.Users, canPage: false); - }

WebGrid Example

- @if (Model.Users != null) + @if (Model != null) { - @MvcHtmlString.Create( - grid.GetHtml( - htmlAttributes: new - { - id = "grid", - @class = "table table-bordered table-striped table-condensed" - }, - emptyRowCellValue: "No Records Found", - headerStyle: "grid-header", - rowStyle: "data-row", - alternatingRowStyle: "alt-row", - columns: grid.Columns( - grid.Column("UserName", "User Name", @@item.Value.UserName), - grid.Column("FirstName", "First Name", @@item.Value.FirstName), - grid.Column("LastName", "Last Name", @@item.Value.LastName), - grid.Column("LastLogin", "Last Login", @@item.Value.LastLogin.ToString()) - ) - ).ToString() - ) +
+ @Html.Partial("userGrid", Model.Users) +
} + diff --git a/WebGridExample/WebGridExample.csproj b/WebGridExample/WebGridExample.csproj index 6c11a2a..d6381d4 100644 --- a/WebGridExample/WebGridExample.csproj +++ b/WebGridExample/WebGridExample.csproj @@ -152,9 +152,11 @@ + +