将下拉列表值作为参数发送给ActionLink

本文关键字:ActionLink 参数 下拉列表 | 更新日期: 2023-09-27 18:16:24

我正在尝试使用@Html。ActionLink并发送我的@Html的值。下拉列表选中的项目。

下面是我的代码:
          <span class="MemberList">
                  @Html.DropDownList("ParticipantList", new SelectList(ViewBag.ParticipantList, "UserName", "UserName", ViewBag.ParticipantList), new { @class = "members" })
          </span>
          <span>
                   @Html.ActionLink("Login", "Impersonate", "Studio", new { username = DropDown.SelectedValue }, new { @class = "gobutton3" })
          </span>

所以我想知道如何将值发送到ActionLink

将下拉列表值作为参数发送给ActionLink

添加动态内容到链接的唯一方法是通过script

<a class="lnkLogin" href="#">Login</a>

然后在你的脚本

$('.lnkLogin').on('click', function(){
    var url = '@Url.Action("Impersonate", "Studio", new { username = "----" })';
    url = url.replace("----", $('#ParticipantList').val());
    window.location = url;
});

this将使用window.location重定向。作为登录,您可能希望在重定向之前执行ajax调用来验证凭据。