Created
October 25, 2018 13:44
-
-
Save andiih/2b7c5f483b8e3db1d365abac6d101491 to your computer and use it in GitHub Desktop.
Kendo Grid MVC Binding Base controller class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using Kendo.Mvc.Extensions; | |
| using Kendo.Mvc.UI; | |
| using Microsoft.AspNetCore.Mvc; | |
| using FiftyEggs.Data.Data; | |
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.EntityFrameworkCore.ChangeTracking; | |
| namespace FiftyEggsSample.Controllers | |
| { | |
| [Authorize] | |
| public class KendoBaseController<T> : Controller where T : class | |
| { | |
| internal readonly SampleContext _context; | |
| public Func<int?, T, bool> FilterFunc; | |
| public KendoBaseController(SampleContext context) | |
| { | |
| _context = context; | |
| FilterFunc = NoFilter; | |
| } | |
| public virtual async Task<ActionResult> Index(int? Id=null) | |
| { | |
| var cn = this.ControllerContext.RouteData.Values["controller"].ToString(); | |
| ViewBag.Title = cn; | |
| ViewBag.Id = Id; | |
| ViewBag.ControllerName = cn; | |
| return View(); | |
| } | |
| public KendoBaseController(SampleContext context, Func<int?, T, bool> filterFunc) | |
| { | |
| _context = context; | |
| FilterFunc = filterFunc; | |
| } | |
| private bool NoFilter(int? id, T thing) | |
| { | |
| return true; | |
| } | |
| public virtual async Task<ActionResult> _Read([DataSourceRequest]DataSourceRequest request, int? id = null) | |
| { | |
| var repo = _context.Set<T>(); | |
| var records = await repo.Where(p => FilterFunc(id, p)).ToListAsync(); | |
| return Json(records.ToDataSourceResult(request)); | |
| } | |
| [HttpPost] | |
| public virtual async Task<ActionResult> _Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<T> records) | |
| { | |
| var results = new List<T>(); | |
| var repo = _context.Set<T>(); | |
| if (records != null && ModelState.IsValid) | |
| { | |
| foreach (var record in records) | |
| { | |
| repo.Add(record); | |
| results.Add(record); | |
| } | |
| TrackSave(_context.ChangeTracker); | |
| await _context.SaveChangesAsync(); | |
| } | |
| return Json(results.ToDataSourceResult(request, ModelState)); | |
| } | |
| public virtual void TrackSave(ChangeTracker contextChangeTracker) | |
| { | |
| } | |
| [HttpPost] | |
| public virtual async Task<ActionResult> _Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<T> records) | |
| { | |
| if (records != null && ModelState.IsValid) | |
| { | |
| foreach (var record in records) | |
| { | |
| _context.Update(record); | |
| } | |
| TrackSave(_context.ChangeTracker); | |
| await _context.SaveChangesAsync(); | |
| } | |
| return Json(records.ToDataSourceResult(request, ModelState)); | |
| } | |
| [HttpPost] | |
| public virtual async Task<ActionResult> _Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<T> records) | |
| { | |
| if (records.Any()) | |
| { | |
| foreach (var record in records) | |
| { | |
| _context.Remove(record); | |
| } | |
| } | |
| TrackSave(_context.ChangeTracker); | |
| await _context.SaveChangesAsync(); | |
| return Json(records.ToDataSourceResult(request, ModelState)); | |
| } | |
| [HttpPost] | |
| public ActionResult Excel_Export_Save(string contentType, string base64, string fileName) | |
| { | |
| var fileContents = Convert.FromBase64String(base64); | |
| return File(fileContents, contentType, fileName); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment