gpt4 book ai didi

javascript - 使用 JavaScript 查询数据库

转载 作者:行者123 更新时间:2023-11-30 00:07:31 24 4
gpt4 key购买 nike

我是 JavaScript 的新手,之前从未使用过 Ajax。我想向 Index.cshtml 添加一个 OnClick,因此当单击一行时,我的代码将查询数据库 (MSSQL) 并返回结果。我在数据库中有两个表,用户和联系人。 User中的主键,UID,是Contact中的外键。这是我的代码:

Controller

  private UserInfoEntities db = new UserInfoEntities();

public ActionResult Index(){
var users = from u in db.User orderby u.UID select u;
return View(users.ToList());
}

查看

@<Info.Model.User>
<script type="text/javascript">
function showEmail() {
var tb = document.getElementById("users");
var rows = tb.rows;
for (var i = 1; i < rows.length; i++) {
rows[i].onclick = (function() {
//some code that query Contact table using UID
// and return Contact.Email in a popup windows or something
});
}
}
</script>
<table class="table" id="users" onclick="showEmail()">
<tr>
<th>
@Html.DisplayNameFor(model => model.NAME)
</th>
<th>
@Html.DisplayNameFor(model => model.UID)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.UID)
</td>
</tr>

感谢任何帮助!

最佳答案

试试这个;

 <script type="text/javascript">
function showEmail() {

//some code that query Contact table using UID
// and return Contact.Email in a popup windows or something

}
</script>
<table class="table" id="users" >
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().NAME)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().UID)
</th>
</tr>
@foreach (var item in Model) {
<tr onclick="showEmail()">
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.UID)
</td>
</tr>
}

关于javascript - 使用 JavaScript 查询数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37935228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com