Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

added 2 characters in body
Source Link
camus
  • 57
  • 1
  • 10

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.htmlcshtml, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UID, is the foreign key in Contact. Here's my code:

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

View

@<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>

Any help is appreciated!

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.html, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UID, is the foreign key in Contact. Here's my code:

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

View

@<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>

Any help is appreciated!

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.cshtml, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UID, is the foreign key in Contact. Here's my code:

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

View

@<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>

Any help is appreciated!

deleted 33 characters in body
Source Link
camus
  • 57
  • 1
  • 10

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.html, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UIDUID, is the foreign key in Contact. Here's my code:

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

View

@<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.FirstOrDefault().NAME)
 </th>
 <th>
 @Html.DisplayNameFor(model => model.FirstOrDefault().UID)
 </th>
 </tr> 
@foreach (var item in Model) {
 <tr>
 <td>
 @Html.DisplayFor(modelItem => item.NAME)
 </td>
 <td>
 @Html.DisplayFor(modelItem => item.UID)
 </td> 
 </tr>

Any help is appreciated!

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.html, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UID is the foreign key in Contact. Here's my code:

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

View

@<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.FirstOrDefault().NAME)
 </th>
 <th>
 @Html.DisplayNameFor(model => model.FirstOrDefault().UID)
 </th>
 </tr> 
@foreach (var item in Model) {
 <tr>
 <td>
 @Html.DisplayFor(modelItem => item.NAME)
 </td>
 <td>
 @Html.DisplayFor(modelItem => item.UID)
 </td> 
 </tr>

Any help is appreciated!

I am fairly new to JavaScript and never used Ajax before. I want to add an OnClick to the Index.html, so when a row is clicked, my code will query database (MSSQL)and return results. I have two tables in database, User and Contact. The primary key in User, UID, is the foreign key in Contact. Here's my code:

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

View

@<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>

Any help is appreciated!

edited tags
Link
user3559349
user3559349
Source Link
camus
  • 57
  • 1
  • 10
Loading
default

AltStyle によって変換されたページ (->オリジナル) /