chore: add DateTimeOffset sample for .net core /telerik/kendo#3678
This commit is contained in:
Родитель
d2ad503310
Коммит
30a963e97a
|
@ -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<CarViewModel>(service.GetAllCars().FirstOrDefault()));
|
||||
}
|
||||
|
||||
public IActionResult AllCars([DataSourceRequest] DataSourceRequest request)
|
||||
{
|
||||
return Json(service.GetAllCars().ToDataSourceResult(request, car => mapper.Map<CarViewModel>(car)));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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<string, List<string>> data = new Dictionary<string, List<string>>();
|
||||
|
||||
public CarsService()
|
||||
{
|
||||
data.Add("BMW", new List<string> {
|
||||
"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<string> {
|
||||
"A1",
|
||||
"A2",
|
||||
"A3",
|
||||
"A4",
|
||||
"A5",
|
||||
"A6",
|
||||
"A7",
|
||||
"A8",
|
||||
"Q1",
|
||||
"Q2",
|
||||
"Q3",
|
||||
"Q5",
|
||||
"Q7"
|
||||
});
|
||||
|
||||
data.Add("Mercedes-Benz", new List<string> {
|
||||
"A",
|
||||
"B",
|
||||
"C",
|
||||
"E",
|
||||
"S",
|
||||
"SL",
|
||||
"ML",
|
||||
"G",
|
||||
"GL",
|
||||
"CLK",
|
||||
"SLK",
|
||||
"SLS",
|
||||
"GLS",
|
||||
"GLE",
|
||||
"GLK",
|
||||
"GLC"
|
||||
});
|
||||
|
||||
data.Add("Volkswagen", new List<string> {
|
||||
"Up",
|
||||
"Lupo",
|
||||
"Polo",
|
||||
"Golf",
|
||||
"Jetta",
|
||||
"Passat",
|
||||
"Arteon",
|
||||
"Caddy",
|
||||
"Touran",
|
||||
"Vento",
|
||||
"Touareg",
|
||||
"Tiguan",
|
||||
"T-Roc",
|
||||
"T-Cross",
|
||||
"Scirocco",
|
||||
"Beetle"
|
||||
});
|
||||
}
|
||||
|
||||
public ICollection<Car> GetAllCars()
|
||||
{
|
||||
var result = new List<Car>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Car, CarViewModel>();
|
||||
CreateMap<CarViewModel, Car>();
|
||||
CreateMap<DateTime, DateTimeOffset>();
|
||||
CreateMap<DateTimeOffset, DateTime>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<CarsService>();
|
||||
services.AddMvc()
|
||||
.AddNewtonsoftJson(options =>
|
||||
options.SerializerSettings.ContractResolver =
|
||||
|
@ -48,6 +57,10 @@ namespace Telerik.Examples.Mvc
|
|||
(options => options.UseSqlServer(connection));
|
||||
|
||||
services.AddKendo();
|
||||
services.Configure<IISServerOptions>(options =>
|
||||
{
|
||||
options.AllowSynchronousIO = true;
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="9.0.0" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
@model CarViewModel
|
||||
|
||||
@Html.LabelFor(x=> x.ProductionDate)
|
||||
@Html.Kendo().DateTimePickerFor(x=> x.ProductionDate)
|
||||
|
||||
@(Html.Kendo().Grid<CarViewModel>()
|
||||
.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"))
|
||||
)
|
||||
)
|
Загрузка…
Ссылка в новой задаче