Introduction

In this blog you will learn how to insert data from input controls into Kendo Grid MVC
Step 1

Create a new project in VS 2015 using Kendo UI ASP.NET MVC 5 Application

Step 2

You will get folder structure and Kendo UI reference into _Layout.cshtml. Now create a new controller. For Action method Index, add new view.
Now create a View Model as to get set data from input controls,
  1. public class EmployeeViewModel
  2. {
  3. public string Name { get; set; }
  4. public string Address { get; set; }
  5. }
Step 3

Add the below code to Index.cshtml,
  1. @using Kendo.Mvc.UI
  2. @using Kendo.Mvc.Extensions
  3. @model KendoUIApp3.ViewModel.EmployeeViewModel
  4. @{
  5. ViewBag.Title = "Index";
  6. Layout = "~/Views/Shared/_Layout.cshtml";
  7. }
  8. <div class="row">
  9. <div class="col-md-12">
  10. <div class="input-group">
  11. @Html.LabelFor(model => model.Name)
  12. @Html.EditorFor(model => model.Name)
  13. @Html.LabelFor(model => model.Address)
  14. @Html.TextBoxFor(model => model.Address)
  15. </div>
  16. </div>
  17. <br />
  18. <div class="col-md-6">
  19. <div class="input-group">
  20. <input type="button" value="submit" id="btnAddToGrid">
  21. </div>
  22. </div>
  23. </div>
  24. <br />
  25. <div>
  26. @(Html.Kendo().Grid<KendoUIApp3.ViewModel.EmployeeViewModel>()
  27. .Name("MyKendoGrid")
  28. .Columns(columns =>
  29. {
  30. columns.Bound(c => c.Name).Width(20);
  31. columns.Bound(c => c.Address).Width(20);
  32. })
  33. .HtmlAttributes(new { style = "height: 350px;width: 500px;" })
  34. .Scrollable()
  35. .Groupable()
  36. .Sortable()
  37. .Pageable(pageable => pageable
  38. .Refresh(true)
  39. .PageSizes(true)
  40. .ButtonCount(5))
  41. .DataSource(dataSource => dataSource
  42. .Ajax()
  43. .PageSize(20)
  44. )
  45. )
  46. </div>
  47. <script>
  48. $("#btnAddToGrid").click(function () {
  49. var name = $('#Name').val();
  50. var add = $('#Address').val();
  51. if (name == "" || add == "") {
  52. alert("Please fill all input fields");
  53. }
  54. var grid = $("#MyKendoGrid").data("kendoGrid");
  55. var datasource = grid.dataSource;
  56. datasource.insert({ Name: name, Address: add });
  57. });
  58. </script>
I have taken input fields values in Javascript code and added those values to Kendo Grid

Step 4

Run this application and you will see,

I hope you have enjoyed this blog.