Skip to content

Commit 040dc1e

Browse files
committed
chore(demos): add remote binding listbox example
1 parent bb3b6b2 commit 040dc1e

File tree

7 files changed

+156
-4
lines changed

7 files changed

+156
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.Linq;
3+
using Telerik.Examples.Mvc.Database;
4+
5+
namespace Telerik.Examples.Mvc.Controllers.ListBox
6+
{
7+
public class RemoteBindingController : Controller
8+
{
9+
private readonly InMemoryDbContext _dbContext;
10+
11+
public RemoteBindingController(InMemoryDbContext dbContext)
12+
{
13+
_dbContext = dbContext;
14+
}
15+
16+
public IActionResult RemoteBinding()
17+
{
18+
return View();
19+
}
20+
21+
public IActionResult GetEmployees()
22+
{
23+
var employees = _dbContext.Employees.ToList();
24+
return Json(employees);
25+
}
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Mvc.Rendering;
2+
using Microsoft.EntityFrameworkCore;
3+
using Telerik.Examples.Mvc.Models;
4+
5+
namespace Telerik.Examples.Mvc.Database
6+
{
7+
public class InMemoryDbContext: DbContext
8+
{
9+
public InMemoryDbContext(DbContextOptions<InMemoryDbContext> options)
10+
: base(options)
11+
{ }
12+
13+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
14+
{
15+
base.OnConfiguring(optionsBuilder);
16+
}
17+
18+
public DbSet<EmployeeViewModel> Employees { get; set; }
19+
}
20+
}

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Models/EmployeeViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
34
using System.Linq;
45
using System.Threading.Tasks;
56

67
namespace Telerik.Examples.Mvc.Models
78
{
89
public class EmployeeViewModel
910
{
11+
[Key]
1012
public int Id { get; set; }
1113

1214
public string Name { get; set; }

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Microsoft.AspNetCore.OData;
1616
using Microsoft.OData.Edm;
1717
using Microsoft.OData.ModelBuilder;
18+
using Telerik.Examples.Mvc.Database;
19+
using Telerik.Examples.Mvc.Seeders;
1820

1921

2022
var builder = WebApplication.CreateBuilder(args);
@@ -49,6 +51,7 @@
4951

5052
builder.Services.Configure<RazorViewEngineOptions>(options =>
5153
{
54+
options.ViewLocationFormats.Add("/Views/ListBox/{0}" + RazorViewEngine.ViewExtension);
5255
options.ViewLocationFormats.Add("/Views/Captcha/{0}" + RazorViewEngine.ViewExtension);
5356
options.ViewLocationFormats.Add("/Views/Grid/{0}" + RazorViewEngine.ViewExtension);
5457
options.ViewLocationFormats.Add("/Views/ImageEditor/{0}" + RazorViewEngine.ViewExtension);
@@ -64,6 +67,10 @@
6467
builder.Services.AddDbContext<GeneralDbContext>(options =>
6568
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
6669

70+
builder.Services.AddDbContext<InMemoryDbContext>(options =>
71+
options.UseInMemoryDatabase("TelerikCoreDb")
72+
);
73+
6774
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
6875
{
6976
options.SignIn.RequireConfirmedAccount = false;
@@ -84,7 +91,8 @@
8491

8592
builder.Services
8693
.AddDistributedMemoryCache()
87-
.AddSession(opts => {
94+
.AddSession(opts =>
95+
{
8896
opts.Cookie.IsEssential = true;
8997
});
9098

@@ -124,9 +132,15 @@
124132
endpoints.MapRazorPages();
125133
});
126134

127-
using var serviceScope = app.Services.CreateScope();
128-
var context = serviceScope.ServiceProvider.GetRequiredService<GeneralDbContext>();
129-
context.Database.Migrate();
135+
136+
using (var serviceScope = app.Services.CreateScope())
137+
{
138+
var context = serviceScope.ServiceProvider.GetRequiredService<GeneralDbContext>();
139+
context.Database.Migrate();
140+
141+
var inMemoryContext = serviceScope.ServiceProvider.GetRequiredService<InMemoryDbContext>();
142+
DataSeeder.SeedListBoxItems(inMemoryContext);
143+
}
130144

131145
app.Run();
132146

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc.Rendering;
2+
using Microsoft.EntityFrameworkCore;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Telerik.Examples.Mvc.Database;
6+
using Telerik.Examples.Mvc.Models;
7+
8+
namespace Telerik.Examples.Mvc.Seeders
9+
{
10+
public class DataSeeder
11+
{
12+
public static void SeedListBoxItems(InMemoryDbContext dbContext)
13+
{
14+
if (dbContext.Employees.Any())
15+
{
16+
return;
17+
}
18+
19+
var employees = new List<EmployeeViewModel>
20+
{
21+
new EmployeeViewModel(){ Id = 1, Name = "Steven White" },
22+
new EmployeeViewModel(){ Id = 2, Name = "Nancy King" },
23+
new EmployeeViewModel(){ Id = 3, Name = "Nancy Davolio" },
24+
new EmployeeViewModel(){ Id = 4, Name = "Michael Leverling" },
25+
new EmployeeViewModel(){ Id = 5, Name = "Andrew Callahan" },
26+
new EmployeeViewModel(){ Id = 6, Name = "Michael Suyama" },
27+
};
28+
29+
dbContext.Employees.AddRange(employees);
30+
dbContext.SaveChanges();
31+
}
32+
}
33+
}

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.2" />
1313
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.2" />
1414
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.2.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.2" />
1516
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.2" />
1617
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.2">
1718
<PrivateAssets>all</PrivateAssets>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<div id="example">
2+
<h3 id="heading">Remote Binding</h3>
3+
<div class="demo-section wide">
4+
@(Html.Kendo().ListBox()
5+
.Name("optional")
6+
.DataTextField("Name")
7+
.DataValueField("Id")
8+
.Toolbar(toolbar =>
9+
{
10+
toolbar.Position(ListBoxToolbarPosition.Right);
11+
toolbar.Tools(tools => tools
12+
.MoveUp()
13+
.MoveDown()
14+
.TransferTo()
15+
.TransferFrom()
16+
.TransferAllTo()
17+
.TransferAllFrom()
18+
.Remove()
19+
);
20+
})
21+
.ConnectWith("selected")
22+
.DataSource(dataSource => dataSource
23+
.Read("GetEmployees", "RemoteBinding")
24+
)
25+
)
26+
27+
@(Html.Kendo().ListBox()
28+
.Name("selected")
29+
.DataTextField("Name")
30+
.DataValueField("Id")
31+
.BindTo(new List<EmployeeViewModel>())
32+
.Selectable(ListBoxSelectable.Multiple)
33+
)
34+
</div>
35+
</div>
36+
37+
<style>
38+
#heading {
39+
justify-content: center;
40+
display: flex;
41+
}
42+
#example .demo-section {
43+
text-align: center;
44+
}
45+
46+
#example .k-listbox {
47+
width: 236px;
48+
height: 360px;
49+
}
50+
51+
#example .k-listbox:first-of-type {
52+
width: 270px;
53+
margin-right: 5px;
54+
}
55+
</style>

0 commit comments

Comments
 (0)