I’m using asp.net MVC3
In my view i’m displaying a table
in table i have a link for each row called “Attach a File”
this link will call controller function through the ajax call
@Html.HiddenFor(modelItem => item.CourseID, new { @id = "CourseID", @Class="courseiding"})
@Html.HiddenFor(modelItem => item.PrtfID, new { @id = "prtfID" , @Class="prtfiding"})
@Html.ActionLink("Attach a file", "Index", null, new { @Id = "attchlink" })
ajax:
$('#attchlink').click(function (e) {
window.formCount = 0;
e.preventDefault();
var id = $('.prtfiding').val();
var size1 = $('.sizing').val();
var url2 = '@Url.Action("Index")';
$.ajax({
url: url2,
data: { pid: id, size: size },
cache: false,
type: 'POST',
success: function (data) {
$('#result').html(data);
}
});
});
This is working for all the rows but while passing value in id and size…it is passing value of first row only for all rows
The Id attribute of an element should be unique on a page.
Your problem is that you have multiple links with the same
Id = "attachlink"and when you call$('#attachlink')jQuery just using the first one.To solve this you should use classes instead of
Id:And then you can use the
.closest()function to get the “colosest” values in your click event:Alternatively you can put all the required data on the ActionLink in the form of
data-attributes:And in your js function you can access them with:
If you don’t need elsewhere the Hidden field you can remove them with this approach.