Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to Kendo UI and I am lost as to why my grid wont display JSON data. Please help

My HTML
C#
<head>
    <title></title>
    <link href="include/libraries/kendo/css/kendo.bootstrap.min.css" rel="stylesheet" />
    <link href="include/libraries/kendo/css/kendo.common-bootstrap.min.css" rel="stylesheet" />

    <script src="include/libraries/jquery/jquery-2.2.2.min.js"></script>
    <script src="include/libraries/kendo/js/kendo.all.min.js"></script>
</head>
<body>

    <div id="example">
            <div class="demo-section k-content wide">
                <div>
                    <h4>Add or update a record</h4>
                    <div data-role="grid">
                         data-editable="true"
                         data-toolbar="['create', 'save']"
                         data-columns="[
                                     { 'field': 'CourseID', 'width': 270 },
                                     { 'field': 'CourseName' },
                                     { 'field': 'IsActive' },
                                  ]"
                         data-bind="source: courses,
                                visible: isVisible,
                                events: {
                                  save: onSave
                                }"
                         style="height: 200px"></div>
                </div>
            </div>

            <script>
                var viewModel = kendo.observable({
                    isVisible: true,
                    courses: new kendo.data.DataSource({
                        schema: {
                            model: {
                                id: "CourseID",
                                fields: {
                                    CourseID: { type: "number" },
                                    CourseName: { type: "string" },
                                    IsActive: { type: "boolean" }
                                }
                            }
                        },
                        batch: true,
                        transport: {
                            read: {
                                type: "GET",
                                cache:false,
                                url: "http://localhost:51447/api/Courses",
                                dataType: "jsonp"
                            },
                            parameterMap: function (options, operation) {
                                if (operation !== "read" && options.models) {
                                    return { models: JSON.stringify(options.models) };
                                }
                            }
                        }
                    })
      
                });
                kendo.bind($("#example"), viewModel);

    </script>

</div>

</body>

My Controller Code
C#
// GET: api/Courses
        public IQueryable<object> GetCourses()
        {
            return db.Courses.Select(
               o => new
               {
                   CourseID = o.CourseID,
                   CourseName = o.CourseName,
                  IsActive = o.IsActive
               });
        }

My JSON data being returned
C#
[{"CourseID":1,"CourseName":"Beauty Therapy","IsActive":true},{"CourseID":2,"CourseName":"Software Development","IsActive":true},{"CourseID":3,"CourseName":"Bookkeeping and Accounting","IsActive":true}]


What I have tried:

I followed the example on Telerik's website http://demos.telerik.com/kendo-ui/grid/mvvm
Posted
Updated 8-Apr-16 0:11am
v2
Comments
Passion4Code 8-Apr-16 5:39am    
Heyy While updating the code format, I guess I missed some code. Can you please post and update the question with the data.
I apologize for this.
S. Kamonere 8-Apr-16 6:07am    
Sorry Passion4Code, but thanks for the interest. I finally got help from another guru and I will post it below
Passion4Code 8-Apr-16 6:35am    
Ok Thanks. :)

1 solution

I finally got this fixed. The structure of my solution is such that it has a WebAPI and a Web app. So in the WebApiConfig.cs file I added the line in bold and it worked.

C#
public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            config.EnableCors();

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900