i am having a situation here … please consider the following scenario
1) value coming from the database eg: personName ( whos id in the database is 3 )
2) i am making a link of the ” personName ” through @Html.ActionLink
3) the link id is “lnkName”
i want to retrieve the id of the personName which is 3 when the lnkName is clicked. please help me ..
here is my code for the link..
@foreach (var item in Model)
{
<h3> @Html.ActionLink(item.personName, "Details", "Post", new { Id = item.personID } , new { @id = "lnkName" } ) </h3>
here is the Jquery code i have got so far…
$(document).ready(function () {
$('#lnkName').click(function () {
$.ajax({
url: @Url.Action("AddViews","Post")
data: // how can i retrieve the id of the clicked link which is 3
});
});
});
When the link is clicked, you can get it’s
idvia thethisvariable:If it’s not the CSS ID that you want here (I wasn’t sure in your question), but some other piece of data like the personID, then you should put that other piece of data as a data attribute on the link tag and fetch that attribute with jQuery using
$(this).data("xxx").For example, if the link generated by your server-side code looked like this:
Then, in your click handler, you can fetch that personID like this (and then you would have to put it into your ajax parameters however you want it to be sent):