c# - 将隐藏字段的值传递给 ActionLink,然后传递给 Controller

标签 c# asp.net asp.net-mvc asp.net-core razor

我正在尝试通过让用户选择他们想要的报告的复选框来动态设置 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/

相关文章:

asp.net - 无法验证数据。在 System.Web.Configuration.MachineKeySection.GetDecodedData

c# - 替换 ASP.NET MVC 中的路由解析器

c# - 如何以 DateTime 格式初始化 8 小时

c# - Java 或 .Net 应用程序是否严重依赖反射?

asp.net - 如何授权在 Asp.Net MVC 中使用外部登录登录的用户

ASP.NET MVC 删除操作方法中的查询字符串

asp.net - 具有相同键的项目已被添加

c# - 如何在 C# 中将数组放入结构中?

c# - WinForms:列表框项目更新不区分大小写?

javascript - 使用 javascript 验证 .net 页面