From 30a963e97a5f219a728376bc37e527b36da843c7 Mon Sep 17 00:00:00 2001 From: Georgi Yankov Date: Tue, 26 May 2020 15:35:11 +0300 Subject: [PATCH] chore: add DateTimeOffset sample for .net core /telerik/kendo#3678 --- .../Grid/DateTimeOffsetController.cs | 35 +++++ .../Telerik.Examples.Mvc/Models/Car.cs | 18 +++ .../Models/CarViewModel.cs | 18 +++ .../Models/CarsService.cs | 128 ++++++++++++++++++ .../Models/MappingProfile.cs | 19 +++ .../Telerik.Examples.Mvc/Startup.cs | 13 ++ .../Telerik.Examples.Mvc.csproj | 2 + .../Views/DateTimeOffset/Index.cshtml | 21 +++ 8 files changed, 254 insertions(+) create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Grid/DateTimeOffsetController.cs create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/Car.cs create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarViewModel.cs create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarsService.cs create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/MappingProfile.cs create mode 100644 Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/DateTimeOffset/Index.cshtml diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Grid/DateTimeOffsetController.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Grid/DateTimeOffsetController.cs new file mode 100644 index 0000000..3b91a3d --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Grid/DateTimeOffsetController.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using Kendo.Mvc.Extensions; +using Kendo.Mvc.UI; +using Microsoft.AspNetCore.Mvc; +using Telerik.Examples.Mvc.Models; + +namespace Telerik.Examples.Mvc.Controllers.Grid +{ + public class DateTimeOffsetController : Controller + { + private readonly IMapper mapper; + private readonly CarsService service; + + + public DateTimeOffsetController(IMapper mapper, CarsService service) + { + this.mapper = mapper; + this.service = service; + } + + public IActionResult Index() + { + return View(mapper.Map(service.GetAllCars().FirstOrDefault())); + } + + public IActionResult AllCars([DataSourceRequest] DataSourceRequest request) + { + return Json(service.GetAllCars().ToDataSourceResult(request, car => mapper.Map(car))); + } + } +} \ No newline at end of file diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/Car.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/Car.cs new file mode 100644 index 0000000..9e467ac --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/Car.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Telerik.Examples.Mvc.Models +{ + public class Car + { + public Guid Id { get; set; } + + public string Make { get; set; } + + public string Model { get; set; } + + public DateTimeOffset ProductionDate { get; set; } + } +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarViewModel.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarViewModel.cs new file mode 100644 index 0000000..f84f866 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarViewModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Telerik.Examples.Mvc.Models +{ + public class CarViewModel + { + public Guid Id { get; set; } + + public string Make { get; set; } + + public string Model { get; set; } + + public DateTime ProductionDate { get; set; } + } +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarsService.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarsService.cs new file mode 100644 index 0000000..8f6669a --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/CarsService.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Telerik.Examples.Mvc.Models +{ + public class CarsService + { + private Random random = new Random(); + private Dictionary> data = new Dictionary>(); + + public CarsService() + { + data.Add("BMW", new List { + "116i", + "116d", + "118i", + "118d", + "120i", + "120d", + "125i", + "125d", + "130i", + "135i", + "235i", + "330i", + "330d", + "335i", + "335d", + "340i", + "430i", + "440i", + "530i", + "530d", + "535i", + "535d", + "540i", + "540d", + "550i", + "550d", + "640i", + "650i", + "750i", + "760i", + "X1", + "X2", + "X3", + "X4", + "x5", + "X6" + }); + + data.Add("Audi", new List { + "A1", + "A2", + "A3", + "A4", + "A5", + "A6", + "A7", + "A8", + "Q1", + "Q2", + "Q3", + "Q5", + "Q7" + }); + + data.Add("Mercedes-Benz", new List { + "A", + "B", + "C", + "E", + "S", + "SL", + "ML", + "G", + "GL", + "CLK", + "SLK", + "SLS", + "GLS", + "GLE", + "GLK", + "GLC" + }); + + data.Add("Volkswagen", new List { + "Up", + "Lupo", + "Polo", + "Golf", + "Jetta", + "Passat", + "Arteon", + "Caddy", + "Touran", + "Vento", + "Touareg", + "Tiguan", + "T-Roc", + "T-Cross", + "Scirocco", + "Beetle" + }); + } + + public ICollection GetAllCars() + { + var result = new List(); + + for (int i = 0; i < 1000; i++) + { + var make = data.ElementAt(random.Next(0, 3)); + result.Add(new Car + { + Id = Guid.NewGuid(), + Make = make.Key, + Model = make.Value[random.Next(0, make.Value.Count)], + ProductionDate = new DateTimeOffset(random.Next(2000,2020), random.Next(1,11), random.Next(1,27), random.Next(1,23), random.Next(1, 59), random.Next(1, 59),TimeSpan.Zero) + }); + } + + return result; + } + } +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/MappingProfile.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/MappingProfile.cs new file mode 100644 index 0000000..0cf8d3c --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/MappingProfile.cs @@ -0,0 +1,19 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Telerik.Examples.Mvc.Models +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Startup.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Startup.cs index 034e6a6..b6a36aa 100644 --- a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Startup.cs +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Startup.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; +using AutoMapper; namespace Telerik.Examples.Mvc { @@ -29,6 +30,14 @@ namespace Telerik.Examples.Mvc { services.AddControllersWithViews(); + var mappingConfig = new MapperConfiguration(mc => + { + mc.AddProfile(new MappingProfile()); + }); + + IMapper mapper = mappingConfig.CreateMapper(); + services.AddSingleton(mapper); + services.AddTransient(); services.AddMvc() .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = @@ -48,6 +57,10 @@ namespace Telerik.Examples.Mvc (options => options.UseSqlServer(connection)); services.AddKendo(); + services.Configure(options => + { + options.AllowSynchronousIO = true; + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj index 61f2f59..01e7248 100644 --- a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj @@ -5,6 +5,8 @@ + + diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/DateTimeOffset/Index.cshtml b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/DateTimeOffset/Index.cshtml new file mode 100644 index 0000000..98fa9f9 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/DateTimeOffset/Index.cshtml @@ -0,0 +1,21 @@ +@model CarViewModel + +@Html.LabelFor(x=> x.ProductionDate) +@Html.Kendo().DateTimePickerFor(x=> x.ProductionDate) + +@(Html.Kendo().Grid() + .Name("grid") + .Columns(columns => { + columns.Bound(column => column.Make); + columns.Bound(column => column.Model); + columns.Bound(column => column.ProductionDate).Format("{0:dd/MM/yyyy HH:mm:ss}"); + }) + .Pageable() + .Filterable() + .Sortable() + .Groupable() + .DataSource(ds => + ds.Ajax() + .Read(r=> r.Action("AllCars", "DateTimeOffset")) + ) + )