表格在进行AJAX请求后未显示数据。

huangapple go评论136阅读模式
英文:

Table not displaying data after making an AJAX request

问题

我正在尝试使用jQuery AJAX从数据库中检索我的数据。我无法弄清楚我的代码有什么问题。我认为我在代码的脚本部分犯了一些错误。任何帮助将不胜感激。

<table class="table">
  <thead>
    <tr>
      <th>
        @Html.DisplayNameFor(model => model.userName)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.password)
      </th>
    </tr>
  </thead>
  <tbody id="AdminList">
    <tr id="loadingStatus" style="color:red">
    </tr>
  </tbody>
</table>
$("#loadingStatus").html("加载中...");
$.get("/Admins/GetAdminData", null, DataBind);

function DataBind(AdminList) {
  var setData = $("#AdminList");
  for (var i = 0; i < AdminList.length; i++) {
    var Data = "<tr class='row_" + AdminList[i].empid + "'>" +
      "<td>" + AdminList[i].username + "</td>" +
      "<td>" + AdminList[i].password + "</td>" +
      "<td>" + "<a href='#' class='btn btn-warning' onclick='EditRecord(" + AdminList[i].empid + ")'><span class='glyphicon glyphicon-edit'></span></a>" + "</td>" +
      "<td>" + "<a href='#' class='btn btn-danger' onclick='DeleteRecord(" + AdminList[i].empid + ")'><span class='glyphicon glyphicon-trash'></span></a>" + "</td>" +
      "</tr>";
    setData.append(Data);
    $("#LoadingStatus").html(" ");
  }
}
public JsonResult GetAdminData() // 需要修复
{
  List<Admin> ad = new List<Admin>();
  var dat = db.Admins.Select(x => new {
    empid = x.empId,
    username = x.userName,
    passWord = x.password
  }).ToList();

  // foreach(DataRow dr in)
  // {
  //   ad.Add(new Admin
  //   {
  //     userName =
  //   });
  // }

  return Json(dat, JsonRequestBehavior.AllowGet);
}
英文:

I'm trying to retrieve my data from a database using jQuery AJAX. I can't figure out what's wrong in my code. I think i did some mistake in the script section of the code. Any help would be most appreciated

<!-- language: lang-html -->

&lt;table class=&quot;table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;
        @Html.DisplayNameFor(model =&gt; model.userName)
      &lt;/th&gt;
      &lt;th&gt;
        @Html.DisplayNameFor(model =&gt; model.password)
      &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody id=&quot;AdminList&quot;&gt;
    &lt;tr id=&quot;loadingStatus&quot; style=&quot;color:red&quot;&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- language: lang-js -->

$(&quot;#loadingStatus&quot;).html(&quot;Loading...&quot;);
$.get(&quot;/Admins/GetAdminData&quot;, null, DataBind);

function DataBind(AdminList) {
  var setData = $(&quot;#AdminList&quot;);
  for (var i = 0; i &lt; AdminList.length; i++) {
    var Data = &quot;&lt;tr class=&#39;row_&quot; + AdminList[i].empid + &quot;&#39;&gt;&quot; +
      &quot;&lt;td&gt;&quot; + AdminList[i].username + &quot;&lt;/td&gt;&quot; +
      &quot;&lt;td&gt;&quot; + AdminList[i].password + &quot;&lt;/td&gt;&quot; +
      &quot;&lt;td&gt;&quot; + &quot;&lt;a href=&#39;#&#39; class=&#39;btn btn-warning&#39; onclick=&#39;EditRecord(&quot; + AdminList[i].empid + &quot;)&#39; &gt;&lt;span class=&#39;glyphicon glyphicon-edit&#39;&gt;&lt;/span&gt;&lt;/a&gt;&quot; + &quot;&lt;/td&gt;&quot; +
      &quot;&lt;td&gt;&quot; + &quot;&lt;a href=&#39;#&#39; class=&#39;btn btn-danger&#39; onclick=&#39;DeleteRecord(&quot; + AdminList[i].empid + &quot;)&#39;&gt;&lt;span class=&#39;glyphicon glyphicon-trash&#39;&gt;&lt;/span&gt;&lt;/a&gt;&quot; + &quot;&lt;/td&gt;&quot; +
      &quot;&lt;/tr&gt;&quot;;
    SetData.append(Data);
    $(&quot;#LoadingStatus&quot;).html(&quot; &quot;);
  }
}

<!-- language: lang-csharp -->

public JsonResult GetAdminData()//needs fixing
{
  List&lt;Admin&gt; ad = new List&lt;Admin&gt;();
  var dat = db.Admins.Select(x =&gt; new {
    empid = x.empId,
    username = x.userName,
    passWord = x.password
  }).ToList();

  //foreach(DataRow dr in)
  //{
  //  ad.Add(new Admin
  //  {
  //    userName =
  //  });
  //}

  return Json(dat, JsonRequestBehavior.AllowGet);
}

答案1

得分: 0

区分大小写的问题,我更改了密码 = x.password

英文:

case sensitive issue i changed

password = x.password

huangapple
  • 本文由 发表于 2020年1月6日 18:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定