2013-02-26 85 views
0

我试图实现电网的基本功能,在编译时我的项目中,我得到一个错误说:“预期的对象”剑道UI电网基本实现

通过以下行代码:

  $(document).ready(function() { 
       $("#grid").kendoGrid({ 
        dataSource: { 
         data: createRandomData(50), //exception in this line of code 
         pageSize: 10 
        }, 

你能帮我理解我需要写的代替createRandomData吗? 它是从哪里获取数据以将其放置在网格上的表名或它是其他内容? (顺便说一句,我用作SQL Server 2008作为后端,并在Visual Studio 2010 MVC4上运行此代码),我也是MVC和Kendo UI的新手。

我想要实现事先使用MVC 4

由于从SQL Server中的数据绑定到网格! :)

下面的代码:

   <script> 
      $(document).ready(function() { 
       $("#grid").kendoGrid({ 
        dataSource: { 
         data: createRandomData(50), 
         pageSize: 10 
        }, 
        groupable: true, 
        sortable: true, 
        pageable: { 
         refresh: true, 
         pageSizes: true 
        }, 
        columns: [{ 
         field: "FirstName", 
         width: 90, 
         title: "First Name" 
        }, { 
         field: "LastName", 
         width: 90, 
         title: "Last Name" 
        }, { 
         width: 100, 
         field: "City" 
        }, { 
         field: "Title" 
        }, { 
         field: "BirthDate", 
         title: "Birth Date", 
         template: '#= kendo.toString(BirthDate,"dd MMMM yyyy") #' 
        }, { 
         width: 50, 
         field: "Age" 
        } 
        ] 
       }); 
      }); 
     </script> 

这里的功能认定中:

function createRandomData(count) { 
     var data = [], 
     now = new Date(); 
     for (var i = 0; i < count; i++) { 
     var firstName = firstNames[Math.floor(Math.random() * firstNames.length)], 
     lastName = lastNames[Math.floor(Math.random() * lastNames.length)], 
     city = cities[Math.floor(Math.random() * cities.length)], 
     title = titles[Math.floor(Math.random() * titles.length)], 
     birthDate = birthDates[Math.floor(Math.random() * birthDates.length)], 
     age = now.getFullYear() - birthDate.getFullYear(); 

    data.push({ 
     Id: i + 1, 
     FirstName: firstName, 
     LastName: lastName, 
     City: city, 
     Title: title, 
     BirthDate: birthDate, 
     Age: age 
    }); 
} 
return data; 

}

应控制器返回作为计数传递的价值?

public ActionResult createRandomData() 
    { 
     return View(); 
    } 

回答

0

函数createRandomData(50)应返回一个json对象。请出示函数的定义也..网格

列名和JSON对象,你回不配套

+0

它的匹配现在正确吗? – subee 2013-02-26 06:09:12

+0

你正在传递对象,没有它..尝试将该数据对象转换成json – 2013-02-26 06:44:37

0

在服务器端,定义一个具有必要属性的类型。然后使用服务器端代码从数据库或使用一些随机数据填充对象数组。从控制器使用内置的JavaScript Serializer将数组序列化为JSON并返回为JsonResult。例如,

公共JsonResult CreateRandomData(诠释计数){

列表人员=

返回新JsonResult(){数据=人,的ContentType = “应用/ JSON”,JsonRequestBehavior = JsonRequestBehavior.AllowGet} ;

}

内置JsonSerializer(System.Web.Mvc)提供JSON序列和deserializatoin支持。