/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/editor-templates.md

https://github.com/sanyaade-mobiledev/kendo-docs · Markdown · 106 lines · 80 code · 26 blank · 0 comment · 0 complexity · 050232c9c59d6f4e07f12b80e5998c4c MD5 · raw file

  1. ---
  2. title: Editor Templates
  3. meta_title: Editor Templates for Kendo UI Grid ASP.NET MVC
  4. meta_description: How to create the editing UI of Kendo Grid ASP.NET MVC with ASP.NET MVC editor templates.
  5. slug: mvc-grid-editor-templates
  6. publish: true
  7. ---
  8. # Editor Templates
  9. ## Introduction
  10. Kendo Grid for ASP.NET MVC relies on ASP.NET MVC editor templates to create the editing UI. If the grid is configured for
  11. in-line or in-cell editing the [Html.EditorFor](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor.aspx) method
  12. is used to get the editor HTML for every property which is editable. So for this configuration:
  13. Html.Kendo().Grid<Order>()
  14. .Name("Grid")
  15. .Columns(columns =>
  16. {
  17. columns.Bound(o => o.OrderDate);
  18. columns.Bound(o => o.ShipCountry);
  19. })
  20. .Editable(editable => editable.Mode(GridEditMode.InLine))
  21. the following code will be used to get the editor HTML for the `OrderDate` and `ShipCountry` properties:
  22. Html.EditorFor(o => o.OrderDate);
  23. Html.EditorFor(o => o.ShipCountry);
  24. If the grid is configured for pop-up editing the [Html.EditorForModel](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorformodel.aspx) is
  25. used to get the editor HTML for the whole model.
  26. A lot of additional info about ASP.NET MVC editor templates can be found in the [ASP.NET MVC 2 Templates](http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html) blog post series.
  27. ## Create Custom Editor For a Bound Property
  28. Often there is need to create a custom editor for a specific property. For example show a dropdownlist which contains all available values a property can take.
  29. This is done by creating an editor template for that property:
  30. 1. Consider the following models which represents the Order and Employee entities from the Northwind database:
  31. public class Order
  32. {
  33. public int OrderID { get; set; }
  34. public string ShipCountry { get; set; }
  35. public Employee Employee { get; set; }
  36. }
  37. public class Employee
  38. {
  39. public int EmployeeID { get; set; }
  40. public string EmployeeName { get; set; }
  41. }
  42. 2. Let's create an editor template for the `Employee` property which will display a Kendo DropDownList with all available employees. Add a new partial view
  43. to the **~/Views/Shared/EditorTemplates** folder e.g. `EmployeeEditor.ascx` or `EmployeeEditor.cshtml` (if using the Razor view engine).
  44. 3. Add a Kendo DropDownList to that partial view. Set the `Name` of the DropDownList to the name of the property which will be edited - `"Employee"` in this case:
  45. - Razor
  46. @(Html.Kendo().DropDownList()
  47. .Name("Employee") // Name of the widget should be the same as the name of the property
  48. .DataValueField("EmployeeID") // The value of the dropdown is taken from the EmployeeID property
  49. .DataTextField("EmployeeName") // The text of the items is taken from the EmployeeName property
  50. .BindTo((System.Collections.IEnumerable)ViewData["employees"]) // A list of all employees which is populated in the controller
  51. )
  52. - WebForms
  53. <%: Html.Kendo().DropDownList()
  54. .Name("Employee") // Name of the widget should be the same as the name of the property
  55. .DataValueField("EmployeeID") // The value of the dropdown is taken from the EmployeeID property
  56. .DataTextField("EmployeeName") // The text of the items is taken from the EmployeeName property
  57. .BindTo((System.Collections.IEnumerable)ViewData["employees"]) // A list of all employees which is populated in the controller
  58. %>
  59. 4. In the action method (which renders the view that contains the grid) populate the ViewData with a list of all employees:
  60. public ActionResult Index()
  61. {
  62. ViewData["employees"] = new NorthwindDataContext()
  63. .Employees
  64. .Select(e => new Employee
  65. {
  66. EmployeeID = e.EmployeeID,
  67. EmployeeName = e.FirstName + " " + e.LastName
  68. })
  69. .OrderBy(e => e.EmployeeName);
  70. return View();
  71. }
  72. 5. Decorate the `Employee` property with the [UIHint](http://msdn.microsoft.com/en-us/library/cc679268) attribute. It needs the name of the editor template created in
  73. step 3 sans the extension i.e. `"EmployeeEditor"`.
  74. public class Order
  75. {
  76. public int OrderID { get; set; }
  77. public string ShipCountry { get; set; }
  78. [UIHint("EmployeeEditor")]
  79. public Employee Employee { get; set; }
  80. }