0

I have this :

<table>
@foreach(var foo in Model.List)
{
    <tr>
        <td><a href="#" class="MnuCustomerSelect" id="@foo.Id">Select</a></td>
        <td>@foo.Id</td>
        <td>@foo.Code</td>
        <td>@foo.LastName</td>
        <td>@foo.FirstName</td>
    </tr>
}
</table>

AS jQuery :

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        $(".MnuCustomerSelect").click(function () {

            var jqxhr = $.post("/Customer/Select", function (data) {
                $(this).Id();
            })
            .success(function () { })
            .error(function () { })
            .complete(function () { });

        });
    });
</script>

The question, I'd like when I click on the selected link, send the corresponding id as argument to /Customer/Select (will be used in my controller)

Any idea ?

Thanks,

Andrew
  • 9,967
  • 10
  • 64
  • 103
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

3 Answers3

1

you can do i easier:

$(document).ready(function () {

    $(".MnuCustomerSelect").click(function () {

        var jqxhr = $.post("/Customer/Select", {id: this.id}, function (data) {
            //do something with data
        })
        .success(function () { })
        .error(function () { })
        .complete(function () { });

    });
});

this.id; is native js to get the DOM id
and u send post elements as the 2nd var in the $.post call

Naftali
  • 144,921
  • 39
  • 244
  • 303
1

While you can, indeed, use $(this).attr('id') to retrieve the id, it's a little faster, and less work, to use the simple, and native to JavaScript, this.id.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

You can use this.id (Thanks David)

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        $(".MnuCustomerSelect").click(function () {

            var jqxhr = $.post("/Customer/Select",{ id: this.id }, function (data) {

            })
            .success(function () { alert('Success!'); })
            .error(function () { alert('Error!'); })
            .complete(function () { alert('Complete!'); });

        });
    });
</script>
Andrew
  • 9,967
  • 10
  • 64
  • 103