Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. [HttpGet]
  2. public ActionResult FetchMyData()
  3. {
  4. List<SomeData> myData = new List<SomeData>();
  5. MyViewModel model = new MyViewModel();
  6. model.myData = new List<SomeData>();
  7.  
  8. model.myData.Add(new SomeData() { Id = 1, Record = "Record 1", RecType = "Type 1" });
  9. model.myData.Add(new SomeData() { Id = 2, Record = "Record 2", RecType = "Type 2" });
  10. model.myData.Add(new SomeData() { Id = 3, Record = "Record 3", RecType = "Type 3" });
  11. model.myData.Add(new SomeData() { Id = 4, Record = "Record 4", RecType = "Type 4" });
  12. model.myData.Add(new SomeData() { Id = 5, Record = "Record 5", RecType = "Type 5" });
  13. JsonResult outputResult = Json(model.myData, JsonRequestBehavior.AllowGet);
  14. return outputResult;
  15. }
  16.  
  17. public class MyViewModel
  18. {
  19. public List<SomeData> myData;
  20. }
  21.  
  22. public class SomeData
  23. {
  24. public int Id;
  25. public string Record;
  26. public string RecType;
  27. }
  28.  
  29. <div data-bind="jqGrid:grid"></div>
  30.  
  31. <script type="text/javascript">
  32. var jsonData;
  33. $(document).ready(function () {
  34. $.ajax({
  35. type: "Get",
  36. url: '@Url.Action("FetchMyData", "Grid")',
  37. dataType: "json",
  38. async: false,
  39. success: function (Data) {
  40. jsonData = Data;
  41. }
  42. });
  43.  
  44. var model = function () {
  45. var self = this;
  46. self.items = ko.observableArray(jsonData);
  47.  
  48. //self.items = ko.observableArray([
  49. // { "Id": 1, Record: "Record 1", Record Type: "Type 1" },
  50. // { "Id": 2, Record: "Record 2", Record Type: "Type 2" },
  51. // { "Id": 3, Record: "Record 3", Record Type: "Type 3" }
  52. //]);
  53.  
  54. self.grid = new ko.jqGrid({
  55. dataSource: self.items,
  56. columns: [
  57. { type: "index", dataField: "Id" },
  58. { headerText: "Id", dataField: "Id" },
  59. { headerText: "Record", dataField: "Record" },
  60. { headerText: "Record Type", dataField: "Record Type" }
  61. ],
  62. allowSorting: true,
  63. });
  64. };
  65. var myModel = new model();
  66. ko.applyBindings(myModel);
  67. });
  68. </script>
  69.  
  70. success: function (Data) {
  71. jsonData = Data;
  72. myModel.items(jsonData);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement