【问题标题】:Jquery Data table displays no datajquery数据表显示无数据
【发布时间】:2017-12-28 12:59:28
【问题描述】:

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

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

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; }
}

GetData() 的屏幕截图

 **ScreenShot for UserData()**

【问题讨论】:

  • 控制台有错误吗?
  • 你错过了映射
  • 不..没有错误..它只显示列名
  • @ArijitMukherjee 你能告诉我我错过了什么以及在哪里吗?
  • 你能展示你的 UserModel 架构吗

标签: c# datatables


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多