Skip to content

Commit 0f6747d

Browse files
committed
chore(examples): add grid editing examples vol2
1 parent 74fc038 commit 0f6747d

File tree

23 files changed

+930
-3
lines changed

23 files changed

+930
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using Telerik.Examples.Mvc.Areas.Grid.Models;
9+
10+
namespace Telerik.Examples.Mvc.Areas.Grid.Controllers
11+
{
12+
public class EditingWithComboBoxRemoteBindingAndFilteringController : Controller
13+
{
14+
private static List<Product> products;
15+
private static List<Category> categories;
16+
17+
static EditingWithComboBoxRemoteBindingAndFilteringController()
18+
{
19+
categories = Enumerable.Range(1, 10)
20+
.Select(i => new Category
21+
{
22+
CategoryID = i,
23+
CategoryName = "Category" + i
24+
}).ToList();
25+
26+
var rand = new Random();
27+
28+
products = Enumerable.Range(1, 20)
29+
.Select(i => new Product
30+
{
31+
ProductId = i,
32+
ProductName = "Product" + i,
33+
Category = categories[rand.Next(9)]
34+
}).ToList();
35+
}
36+
37+
public ActionResult Index()
38+
{
39+
return View();
40+
}
41+
42+
public ActionResult Read([DataSourceRequest] DataSourceRequest request)
43+
{
44+
return Json(products.ToDataSourceResult(request));
45+
}
46+
47+
public ActionResult Update([DataSourceRequest] DataSourceRequest request, Product product)
48+
{
49+
if (product != null && ModelState.IsValid)
50+
{
51+
var target = products.First(p => p.ProductId == product.ProductId);
52+
target.ProductName = product.ProductName;
53+
target.Category = product.Category;
54+
55+
//TODO: save the changes
56+
}
57+
58+
return Json(new[] { product }.ToDataSourceResult(request));
59+
}
60+
61+
//Method is designed to return empty array if no text is sent to the server.
62+
//This is helpful if you have a great amount of data and want to return only a filtered set.
63+
public ActionResult _GetCategories(string text)
64+
{
65+
var data = new List<Category>();
66+
67+
if (!string.IsNullOrEmpty(text))
68+
{
69+
data = categories.Where(p => p.CategoryName.ToLower().Contains(text.ToLower())).ToList();
70+
}
71+
72+
return Json(data, JsonRequestBehavior.AllowGet);
73+
}
74+
}
75+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using Telerik.Examples.Mvc.Areas.Grid.Models;
9+
10+
namespace Telerik.Examples.Mvc.Areas.Grid.Controllers
11+
{
12+
public class InCellEditingWithCascadingDropDownListsController : Controller
13+
{
14+
public ActionResult Index()
15+
{
16+
var values = new ForeignKeyValues
17+
{
18+
Customers = CustomerRepository.Repository.Customers,
19+
Vendors = VendorRepository.Repository.Vendors,
20+
Products = ProductRepository.Repository.Products
21+
};
22+
return View(values);
23+
}
24+
25+
[HttpPost]
26+
public JsonResult Read([DataSourceRequest] DataSourceRequest request)
27+
{
28+
return Json(LicenseRepository.Repository.GetAll().ToDataSourceResult(request));
29+
}
30+
31+
[HttpPost]
32+
public JsonResult Create([DataSourceRequest] DataSourceRequest request, License license)
33+
{
34+
if (license != null && ModelState.IsValid)
35+
{
36+
LicenseRepository.Repository.Insert(license);
37+
}
38+
39+
return Json(new[] { license }.ToDataSourceResult(request, ModelState));
40+
}
41+
42+
[HttpPost]
43+
public JsonResult Update([DataSourceRequest] DataSourceRequest request, License license)
44+
{
45+
if (license != null && ModelState.IsValid)
46+
{
47+
LicenseRepository.Repository.Update(license);
48+
}
49+
50+
return Json(ModelState.ToDataSourceResult());
51+
}
52+
53+
public JsonResult GetCustomers()
54+
{
55+
return Json(CustomerRepository.Repository.Customers, JsonRequestBehavior.AllowGet);
56+
}
57+
58+
public JsonResult GetVendors(int customerId)
59+
{
60+
return Json(VendorRepository.Repository.GetVendorsByCustomer(customerId), JsonRequestBehavior.AllowGet);
61+
}
62+
63+
public JsonResult GetProducts(int vendorId)
64+
{
65+
return Json(ProductRepository.Repository.GetProductsByVendor(vendorId), JsonRequestBehavior.AllowGet);
66+
}
67+
}
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using Telerik.Examples.Mvc.Models;
9+
10+
namespace Telerik.Examples.Mvc.Areas.Grid.Controllers
11+
{
12+
public class InCellEditingWithRadioButtonColumnController : Controller
13+
{
14+
public static List<Person> persons = new List<Person>();
15+
16+
static InCellEditingWithRadioButtonColumnController()
17+
{
18+
persons.Add(new Person { PersonID = 1, Name = "John", BirthDate = new DateTime(1968, 6, 26), Role = Role.SuperUser });
19+
persons.Add(new Person { PersonID = 2, Name = "Sara", BirthDate = new DateTime(1974, 9, 13) });
20+
persons.Add(new Person { PersonID = 3, Name = "Peter", BirthDate = new DateTime(1976, 4, 20), Role = Role.User });
21+
}
22+
23+
public ActionResult Index()
24+
{
25+
return View();
26+
}
27+
28+
public ActionResult GetPersons([DataSourceRequest] DataSourceRequest dsRequest)
29+
{
30+
var result = persons.ToDataSourceResult(dsRequest);
31+
return Json(result);
32+
}
33+
34+
[AcceptVerbs(HttpVerbs.Post)]
35+
public ActionResult UpdatePersons([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable<Person> updated)
36+
{
37+
if (updated != null && ModelState.IsValid)
38+
{
39+
foreach (Person person in updated)
40+
{
41+
var target = persons.FirstOrDefault(p => p.PersonID == person.PersonID);
42+
if (target != null)
43+
{
44+
target.Name = person.Name;
45+
target.BirthDate = person.BirthDate;
46+
target.Role = person.Role;
47+
}
48+
}
49+
}
50+
51+
return Json(ModelState.ToDataSourceResult());
52+
}
53+
}
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using Telerik.Examples.Mvc.Areas.Grid.Models;
9+
10+
namespace Telerik.Examples.Mvc.Areas.Grid.Controllers
11+
{
12+
public class InLineEditingWithNullableBooleanController : Controller
13+
{
14+
public static IList<InLineEditingProduct> Products
15+
{
16+
get;
17+
set;
18+
}
19+
20+
static InLineEditingWithNullableBooleanController()
21+
{
22+
var random = new Random();
23+
24+
Products = Enumerable.Range(0, 20).Select(index => new InLineEditingProduct
25+
{
26+
ProductID = index + 1,
27+
ProductName = "Product " + index,
28+
UnitsInStock = random.Next(20, 200),
29+
Available = index % 3 == 0 ? (bool?)null : index % 2 == 0
30+
}).ToList();
31+
}
32+
33+
public ActionResult Index()
34+
{
35+
ViewBag.Message = "Welcome to ASP.NET MVC!";
36+
37+
return View();
38+
}
39+
40+
public ActionResult Read([DataSourceRequest] DataSourceRequest request)
41+
{
42+
return Json(Products.ToDataSourceResult(request));
43+
}
44+
45+
public ActionResult Update([DataSourceRequest] DataSourceRequest request, InLineEditingProduct product)
46+
{
47+
if (ModelState.IsValid)
48+
{
49+
var dataItem = Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();
50+
51+
dataItem.ProductID = product.ProductID;
52+
dataItem.ProductName = product.ProductName;
53+
dataItem.UnitsInStock = product.UnitsInStock;
54+
dataItem.Available = product.Available;
55+
}
56+
57+
// Return the updated entities as well as any validation errors.
58+
return Json(new[] { product }.ToDataSourceResult(request, ModelState));
59+
}
60+
}
61+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using Telerik.Examples.Mvc.Models;
9+
10+
namespace Telerik.Examples.Mvc.Areas.Grid.Controllers
11+
{
12+
public class PopUpEditingWithRequiredDropDownListForController : Controller
13+
{
14+
public static List<Person> persons = new List<Person>();
15+
16+
static PopUpEditingWithRequiredDropDownListForController()
17+
{
18+
persons.Add(new Person { PersonID = 1, Name = "John", Gender = "Male", Country = "England", BirthDate = new DateTime(1968, 6, 26) });
19+
persons.Add(new Person { PersonID = 2, Name = "Sara", Gender = "Female", Country = "Spain", BirthDate = new DateTime(1974, 9, 13) });
20+
}
21+
22+
public ActionResult Index()
23+
{
24+
return View();
25+
}
26+
27+
public ActionResult GetPersons([DataSourceRequest] DataSourceRequest dsRequest)
28+
{
29+
var result = persons.ToDataSourceResult(dsRequest);
30+
return Json(result);
31+
}
32+
33+
public ActionResult DestroyPerson([DataSourceRequest] DataSourceRequest dsRequest, Person person)
34+
{
35+
if (person != null && ModelState.IsValid)
36+
{
37+
persons.Remove(persons.FirstOrDefault(p => p.PersonID == person.PersonID));
38+
}
39+
40+
return Json(ModelState.ToDataSourceResult());
41+
}
42+
43+
public ActionResult CreatePerson([DataSourceRequest] DataSourceRequest dsRequest, Person person)
44+
{
45+
if (person != null && ModelState.IsValid)
46+
{
47+
var id = persons.Last().PersonID + 1;
48+
var newPerson = new Person()
49+
{
50+
PersonID = id,
51+
BirthDate = person.BirthDate,
52+
Name = person.Name,
53+
Country = person.Country,
54+
Gender = person.Gender
55+
};
56+
57+
persons.Add(newPerson);
58+
person.PersonID = id;
59+
}
60+
61+
return Json(ModelState.ToDataSourceResult());
62+
}
63+
64+
public ActionResult UpdatePerson([DataSourceRequest] DataSourceRequest dsRequest, Person person)
65+
{
66+
if (person != null && ModelState.IsValid)
67+
{
68+
var toUpdate = persons.FirstOrDefault(p => p.PersonID == person.PersonID);
69+
TryUpdateModel(toUpdate);
70+
}
71+
72+
73+
return Json(ModelState.ToDataSourceResult());
74+
}
75+
}
76+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace Telerik.Examples.Mvc.Areas.Grid.Models
7+
{
8+
public class Category
9+
{
10+
public int CategoryID { get; set; }
11+
public string CategoryName { get; set; }
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace Telerik.Examples.Mvc.Areas.Grid.Models
7+
{
8+
public class ForeignKeyValues
9+
{
10+
public IEnumerable<Customer> Customers { get; set; }
11+
public IEnumerable<Vendor> Vendors { get; set; }
12+
public IEnumerable<Product> Products { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)