我正在尝试通过让用户选择他们想要的报告的复选框来动态设置 StudentId。当他们单击 ActionLink 时,我使用 jQuery 在隐藏字段中设置 id 的值。我需要操作链接来读取隐藏字段中的 ID。
值在隐藏字段中设置,但它们没有从 actionLink 传递到 Controller 。
我需要将 ReportId 传递给 Controller 。
@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", new { studentIdList = Model.ReportIds }, new { @class = "button print_selected print_selectedMedEd disabled" })
@Html.HiddenFor(model => model.ReportIds)
JavaScript
$('.print_selectedMedEd').bind('click', function () {
var ids = getPrintIds();
if (ids.length == 0) {
return false;
}
$('#ReportIds').val(ids.toString());
return true;
});
最佳答案
当 asp.net mvc 呈现您的 ActionLink
时,它将生成一个带有您在模型上的参数的链接。如果您更改模型的值,它不会更改 ActionLink
生成的输出值。
鉴于此,您必须再次获取该值,尝试生成一个不带参数的 ACtionLink:
@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", null, new { @class = "button print_selected print_selectedMedEd disabled" })
在 javascript 方面,您可以尝试使用 on
方法绑定(bind)事件处理程序并从事件参数调用 preventDefault
方法,例如:
$('.print_selectedMedEd').on('click', function (e) {
e.preventDefault();
var ids = getPrintIds();
if (ids.length > 0) {
$('#@Html.IdFor(model => model.ReportIds)').val(ids.toString());
// make an url to redirect
var url = $(this).prop("href") + "?studentIdList=" + ids.toString();
// redirect using the generated url
window.location.href = url;
}
});
请记住使用 Html.IdForm()
来确保您拥有特定属性的正确 ID。
关于c# - 将隐藏字段的值传递给 ActionLink,然后传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391842/