Jquery数据表不显示任何数据

时间:2017-12-28 12:59:29

标签: c# datatables

我的要求是从活动目录中获取用户的详细信息,然后将其保存在列表中并使用Jquery Data选项卡显示它。我已经设法从Active Directory中检索数据..我能够将数据放入List ..我现在必须使用Jquery Data表显示它。当我运行View时,它只显示列的名称但是没有显示任何数据...

请查看以下代码以供参考。

using ActiveDirectory.Models;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ActiveDirectory.Controllers 
{
    public class DisplayUserController : Controller
    {
        // GET: DisplayUser

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

[HttpGet]

public ActionResult UsersData()
{
    List<UsersModel> lstADUsers = new List<UsersModel>();
    string DomainPath = "LDAP://SGZ";
    DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
    DirectorySearcher search = new DirectorySearcher(searchRoot);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";
    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("usergroup");
    search.PropertiesToLoad.Add("displayname");//first name
    SearchResult result;
    SearchResultCollection resultCol = search.FindAll();
    if (resultCol != null)
    {
        for (int counter = 0; counter < resultCol.Count; counter++)
        {
            string UserNameEmailString = string.Empty;
            result = resultCol[counter];
            if (result.Properties.Contains("samaccountname") &&
            result.Properties.Contains("mail") &&
            result.Properties.Contains("displayname"))
            {
                UsersModel objSurveyUsers = new UsersModel();
            objSurveyUsers.Email = (String)result.Properties["mail"][0] +
"^" + (String)result.Properties["displayname"][0];
                objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
                objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
                lstADUsers.Add(objSurveyUsers);
            }
        }
    }

    return Json(new { data = lstADUsers }, JsonRequestBehavior.AllowGet);

    }

    }

}

查看:

@model ActiveDirectory.Models.UsersModel
@{
ViewBag.Title = "User Details";
}

<div>

<style>
table, th, td
{
    border: 1px solid black;
    border-collapse: collapse;
    align-content: center;
}
</style>
<div style="border:solid;width:100%;overflow-x:auto;">
    <table id="table" align="center" style="width:100%" class="display">
    <thead>
    <tr>
        <th>Display Name</th>
        <th>User Name</th>
        <th>Email</th>
        <th>Mappiing</th>
    </tr>
    </thead>
</table>
</div>
    <input type="submit" name="submit" value="submit" />
</div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script 
src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js">
</script>
<script type="text/javascript">
$(document).ready(function ()
{
    $('#table').DataTable({
    "ajax": {
    "url": "@Url.Action("UsersData", "DisplayUser")", // action method URL
    "type": "GET",
    "datatype": "json"
},
columns: [
        { "data": "Display Name" },
        { "data": "User name" },
        { "data": "Email" },
        { "data": "Mapping" },

        ],
// other settings
    });
});
</script>

用户模型

public class UsersModel
{
    public string Email { get; set; }
    public string UserName { get; set; }
    public string DisplayName { get; set; }
    public bool isMapped { get; set; }
}

ScreenShot for GetData()

 **ScreenShot for UserData()**

enter image description here

1 个答案:

答案 0 :(得分:0)

响应中的数据属性名称和初始化代码不匹配。

columns初始化选项使用以下代码。

"columns": [
    { "data": "DisplayName" },
    { "data": "UserName" },
    { "data": "Email" },
    { "data": "isMapped" }
]