[英]Pass model item id to jquery function from Html.ActionLink
我希望能够使用Html.ActionLink()将以下代码中每个项目的ID传递给jQuery函数。
@foreach (var item in Model)
{
if (item.AppointmentStatus == "P")
{
<div class="appointment-main-block" role="menuitem" tabindex="20">
<div class="appointment-heading">
@Html.DisplayFor(modelItem => item.Speciality)
</div>
<div class="appointmenttitle">
Date
<br />
<br />
Clinician
<br />
<br />
Location
</div>
<div class="appointmentdata">
@Html.DisplayFor(modelItem => item.AppointmentDate)
<br />
<br />
@Html.DisplayFor(modelItem => item.ClinicianName)
<br />
<br />
@Html.DisplayFor(modelItem => item.Location)
</div>
<div class="appointmentbutton appointmentbuttonclickable">
View @Html.ActionLink(" ", "Details", "MyAppointments", new { id = item.AppointmentIdentifier.id }, new { onclick = "getAppointmentRef( item.AppointmentIdentifier.id)" })
<br />
<img src="/Content/images/icons/view.png" alt="view icon" />
</div>
</div>
}
}
// jQuery - Very Basic for now also considered
$(".appointmentbutton").click(getAppointmentRef);
function getAppointmentRef(id) {
alert(id);
}
该函数称为ok,但是即使我传递了“ hello”,它似乎也传递了JSON字符串,其参数都不是“ id”。 123456789
如何使Actionlink拾取该模型项目的ID?
首先,您在这里存在语法错误。 我认为,应改为:
onclick = "getAppointmentRef( @item.AppointmentIdentifier.id)"
要么
onclick = "getAppointmentRef( " + @item.AppointmentIdentifier.id + ")"
两者之一。
但是,原则上我会采用不同的方法:
@Html.ActionLink("Link text", "Details", "MyAppointments", new { id = item.AppointmentIdentifier.id }, new { @class = "myLink", data_id = item.AppointmentIdentifier.id })
然后在jQuery中:
$(".myLink").click(function () {
alert($(this).data("id"));
});
这样,您就可以更清晰地将关注点分离。
我希望这有帮助。
对于按钮,您可以使用类似-
<input type="button" data-apptid="@item.AppointmentIdentifier.id" class="myLink" value="Click me!" />
您的jQuery可能就像Moby提到的那样-
$(".myLink").click(function () {
document.location.href = "/MyAppointments/Details/" & $(this).data("apptid");
});
这样,如果除了加载新页面外,还需要在jQuery中执行某些操作,则可以在其中执行,并让函数确定是否重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.