如何MS SQL数据库记录转换为JSON记录在网页API

问题描述:

时想显示在网页API调用w2ui一格,是新的网络API,所以当我做这个很getiing如何MS SQL数据库记录转换为JSON记录在网页API

"ContentEncoding": null, 
    "ContentType": null, 
    "Data": [ 
    { 
     "Id": 1, 
     "Name": "test guy", 
     "Address": "20 Glover Avenue", 
     "Email": "[email protected]", 
     "Postal": "06850" 
    }, 

我如何改变“ContentType”:json?并发送至w2ui电网

任何帮助,我怎样才能在网上API调用返回JSON结果?

的WebAPI控制器

public JsonResult GetAll() 
     { 
      return _repository.GetAll(); 
     } 

JsonResult GetAll(); 
public JsonResult GetAll() 
     { 
      JsonResult JsonData = new JsonResult(); 
      JsonData.Data = db.UserAccounts.ToList(); 
      return JsonData; 

     } 

w2ui视图

$(function() { 
     $('#grid') 
      .w2grid({ 
       name: 'grid', 
       url: 'api/UserAcc', 
       method: 'GET', 
       columns: [ 
        { field: 'Name', name:'', caption: 'Name', size: '100%' }, 
        { field: 'email', caption: 'Email', size: '100%' }, 
        { field: 'Address', caption: 'Address', size: '120px', render: 'money' }, 
        { field: 'Postal', caption: 'Postal', size: '120px', render: 'date' } 
       ] 

      }); 

"ContentEncoding": null, 
"ContentType": null, 
"Data": [ 
{ 
    "Id": 1, 
    "Name": "test guy", 
    "Address": "20 Glover Avenue", 
    "Email": "[email protected]", 
    "Postal": "06850" 
} 

这是整个的HttpResponse。此响应已被格式化为json。要使用的数据是名为“Data”的数组对象,其中的数据也是json格式。

"ContentType":"application/json" 

另外,您应该将内容类型更改为application/json,以便浏览器明白服务器正在发送json对象。

欲了解更多信息,你可以看看这个例子的Web API:Getting started with ASP.NET Web API 2

我希望我回答你的问题。

UPDATE

我看着W2UI文档,发现这个例子

$(function() { 
    $('#grid').w2grid({ 
     name: 'grid', 
     header: 'List of Names', 
     columns: [ 
       { field: 'Name', name:'', caption: 'Name', size: '100%' }, 
       { field: 'email', caption: 'Email', size: '100%' }, 
       { field: 'Address', caption: 'Address', size: '120px', render: 'money' }, 
       { field: 'Postal', caption: 'Postal', size: '120px', render: 'date' } 
     ], 
     records: [ 
      { Id: 1, Name: "test guy", Address: "20 Glover venue",Email:"[email protected]",Postal:"06850"}   
     ] 
    }); 
}); 

因此,从网络API的记录应放入记录财产

+0

请我的问题我已经更新了它 –

+0

@MNMOHAMED {field:'email',caption:'Email',size:'100%'}在外地财产不应该有电子邮件与Captial信件? –

+0

已完成@Imants Volkovs –