WCF数据服务HTML

时间:2013-10-16 11:28:41

标签: html wcf

我想创建一种机制,允许用户在他们自己的网页中嵌入他们的出版物的简单列表(使用UL和LI)。

数据源是SSRS数据库,所以我在考虑使用WCF数据服务。但我发现WCF数据服务只返回ATOM或JSON数据。

我是在咆哮错误的树吗?

1 个答案:

答案 0 :(得分:0)

为什么不使用返回的JSON数据并使用MustacheJs或任何其他javascript模板工具将数据显示为UL和Li

以下代码我使用的是web api,这是一种简单的wcf数据服务形式。你可以使用任何返回JSON数据的东西。

胡子模板

<script id="RoleTemplate" type="Mustache/html">
  {{ #roles }}
         <UL>
              <Li>{{RoleID}} - {{RoleName}} ({{Description}}) </Li>
         </UL>    
{{ /roles }}

Web api控制器代码

   public class SecurityController : ApiController
{


    // GET api/<controller>
    /// <summary>
    /// Get all role details
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public List<Role> GetAllRoles()
    {
        myApp.Security.Services.ISecurityService objSecurity = new myApp.Security.Services.SecurityService();
        return objSecurity.GetAllRole();
    }
}

调用Ajax

     function getAllRoles() {
         $.ajax({
             dataType: "json",
             url: '/api/Security/GetAllRole',
             success: function (data) {
                 data = { 'roles': data }; // formatting the data to support the mustache format 
                 var html = Mustache.to_html($('#RoleTemplate').html(), data);
                 $('#tblRole').append(html);


             }
         });