views:

51

answers:

3

Hello,

I have this code

<% foreach (var item in Model.List) { %>    
    <tr>
        <td><%: item.LastName %></td>
        <td><%: item.FirstName %></td>
        <td><%: item.IsEnable %></td>
        <td><a href="#" class="CustomerEdit">Edit</a></td>
        <td><a href="#" class="CustomerDetail">Detail</a></td>
        <td><a href="#" class="CustomerDelete">Delete</a></td>
    </tr>    
<% } %>


<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $(".CustomerEdit").click(function () {
            alert("blabla");
            //need id here
        });
    });
</script>

It's not in the code but I have an "Item.Id", it's not place anywhere because I don't know where place it ;-). I'd like when I click on the "Edit" hyperlink get the id (item.Id) of the current line.

Any idea ?

Thanks,

A: 

You could just add a Javascript function at that point

For instance

<a href="#" class="CustomerEdit" onclick="getdata(<%: item.Id%>); return false;">Edit</a>

If Id=11 This will call getdata(11) when you click on Edit. What you do in getdata then, it's up to you!

EDIT: actually, you should probably just use a <div> instead of <a> if those link don't actually link to anything.

nico
+1  A: 
<a href="#" class="CustomerEdit" id="edit_<%: item.Id %>">Edit</a>

and then read the id:

$(function() {
    $(".CustomerEdit").click(function() {
        var id = this.id.replace(/edit_/, ''); 
        alert(id);
        return false;
    });
});
Darin Dimitrov
A: 
<% foreach (var item in Model.List) { %>    
  <tr id="<%: item.Id %>">
    <td><%: item.LastName %></td>
    <td><%: item.FirstName %></td>
    <td><%: item.IsEnable %></td>
    <td><a href="#" class="CustomerEdit">Edit</a></td>
    <td><a href="#" class="CustomerDetail">Detail</a></td>
    <td><a href="#" class="CustomerDelete">Delete</a></td>
  </tr>    
<% } %>


<script language="javascript" type="text/javascript">
  $(document).ready(function () {
    $(".CustomerEdit").click(function () {
      var itemID = $(this).parent("tr").attr("id");
    });
  });
</script>

NOTE: Untested, but I think that should work.

Lucanos