Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpGet]
- public ActionResult FetchMyData()
- {
- List<SomeData> myData = new List<SomeData>();
- MyViewModel model = new MyViewModel();
- model.myData = new List<SomeData>();
- model.myData.Add(new SomeData() { Id = 1, Record = "Record 1", RecType = "Type 1" });
- model.myData.Add(new SomeData() { Id = 2, Record = "Record 2", RecType = "Type 2" });
- model.myData.Add(new SomeData() { Id = 3, Record = "Record 3", RecType = "Type 3" });
- model.myData.Add(new SomeData() { Id = 4, Record = "Record 4", RecType = "Type 4" });
- model.myData.Add(new SomeData() { Id = 5, Record = "Record 5", RecType = "Type 5" });
- JsonResult outputResult = Json(model.myData, JsonRequestBehavior.AllowGet);
- return outputResult;
- }
- public class MyViewModel
- {
- public List<SomeData> myData;
- }
- public class SomeData
- {
- public int Id;
- public string Record;
- public string RecType;
- }
- <div data-bind="jqGrid:grid"></div>
- <script type="text/javascript">
- var jsonData;
- $(document).ready(function () {
- $.ajax({
- type: "Get",
- url: '@Url.Action("FetchMyData", "Grid")',
- dataType: "json",
- async: false,
- success: function (Data) {
- jsonData = Data;
- }
- });
- var model = function () {
- var self = this;
- self.items = ko.observableArray(jsonData);
- //self.items = ko.observableArray([
- // { "Id": 1, Record: "Record 1", Record Type: "Type 1" },
- // { "Id": 2, Record: "Record 2", Record Type: "Type 2" },
- // { "Id": 3, Record: "Record 3", Record Type: "Type 3" }
- //]);
- self.grid = new ko.jqGrid({
- dataSource: self.items,
- columns: [
- { type: "index", dataField: "Id" },
- { headerText: "Id", dataField: "Id" },
- { headerText: "Record", dataField: "Record" },
- { headerText: "Record Type", dataField: "Record Type" }
- ],
- allowSorting: true,
- });
- };
- var myModel = new model();
- ko.applyBindings(myModel);
- });
- </script>
- success: function (Data) {
- jsonData = Data;
- myModel.items(jsonData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement