将值从隐藏字段传递到 ActionLink,然后传递到 Controller

Ron*_*ald 1 c# asp.net asp.net-mvc razor asp.net-core

我试图通过让用户选择他们想要的报告的复选框来动态设置 StudentId。当他们单击 ActionLink 时,我使用 jQuery 在隐藏字段中设置 id 的值。我需要操作链接来读取隐藏字段中的 id。

这些值在隐藏字段中设置,但它们没有从 actionLink 传递到控制器。

我需要将 ReportIds 传递给控制器​​。

        @Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", new { studentIdList = Model.ReportIds }, new { @class = "button print_selected print_selectedMedEd disabled" })



        @Html.HiddenFor(model => model.ReportIds)
Run Code Online (Sandbox Code Playgroud)

javascript

$('.print_selectedMedEd').bind('click', function () {
    var ids = getPrintIds();
    if (ids.length == 0) {
        return false;
    }

    $('#ReportIds').val(ids.toString());


    return true;
});
Run Code Online (Sandbox Code Playgroud)

Fel*_*ani 5

当 asp.net mvc 渲染您时,ActionLink它将生成一个包含模型参数的链接。如果您更改模型的值,它不会更改 生成的输出上的值ActionLink

鉴于此,您必须再次设置该值,尝试生成一个不带参数的 ACtionLink:

@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", null, new { @class = "button print_selected print_selectedMedEd disabled" })
Run Code Online (Sandbox Code Playgroud)

在 javascript 方面,您可以尝试使用该on方法绑定事件处理程序并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;
    }
});
Run Code Online (Sandbox Code Playgroud)

请记住使用 来Html.IdForm()确保您拥有特定属性的正确 ID。