我在我的视图中使用asp.net文件,在表中显示一个表,每个行都有一个名为"Attach a MVC3“的链接。这个链接将通过ajax调用调用控制器函数
@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);
}
});
});这对所有行都有效,但是当传递id和size...it中的值时,只传递所有行的第一行的值
发布于 2012-06-08 14:45:15
元素的Id attribute在页面上应该是唯一的。
您的问题是,您有多个具有相同Id = "attachlink"的链接,并且当您调用$('#attachlink') jQuery时,只使用第一个链接。
为了解决这个问题,你应该使用类而不是Id
@Html.ActionLink("Attach a file", "Index", null, new { @class = "attachlink" })
$('.attachlink').click(function (e) {
}然后,您可以使用.closest()函数在单击事件中获取"colosest“值:
var id = $(this).closest('.prtfiding').val();
var size1 = $(this).closest('.sizing').val();或者,您可以将所有需要的数据以data-属性的形式放在ActionLink上:
@Html.ActionLink("Attach a file", "Index", null,
new
{
@Id = "attchlink",
data_courseID = item.CourseID,
data_prtfID = item.prtfID
})在您的js函数中,您可以使用以下命令访问它们:
var courseID = $(this).data('courseID');
var prtfID = $(this).data('prtfID');如果您不需要在其他地方使用Hidden字段,则可以使用此方法删除它们。
https://stackoverflow.com/questions/10944095
复制相似问题