【问题标题】:How to click ActionLink automatically when the dropdown is changes?如何在下拉列表更改时自动单击 ActionLink?
【发布时间】:2019-05-20 03:59:30
【问题描述】:

我有一个下拉菜单和一个操作链接。 当下拉列表更改时,将自动单击此操作链接。怎么做?。下面是我的代码,谢谢。

   @Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control" })
   @Html.ActionLink(
                       "Detail",
                       "GetInsuranceCompany","ParamBlacklistPembayaran",
                       new { id = Model.PaymentCode }, new { @class = "ddlSubmit"})

控制器

public ActionResult GetInsuranceCompany( ParamBlacklistPembayaranViewModel model,string id)
{ 
  LoadThirdPartyDDL(string.Empty, string.Empty, id);
  return View("Create", model);
}

【问题讨论】:

    标签: javascript asp.net-mvc


    【解决方案1】:
     @Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control",@id="ddl" })
    
    
    @Html.ActionLink("Detail",
                           "GetInsuranceCompany","ParamBlacklistPembayaran",
                           new { id = "PaymentCodeVal" }, new { @id="anchorclick",@class = "ddlSubmit"})
    

    您应该像这样在下拉更改时调用点击事件:

     <script>
            document.getElementById('ddl').onchange = function () {
             var  path =  document.getElementById('anchorclick').href;
                          path = path.replace("PaymentCodeVal", document.getElementById('ddl').value);
                          document.getElementById("anchorclick").href=path; 
                          document.getElementById('anchorclick').click();
        };
     </script>
    

    @NOTE :您想获得更新的 PaymentCode。你必须注入 url 才能在 change 事件中传递 PaymentCode。

    【讨论】:

    • 它不工作。如果单击 actionlink,我会在控制器中设置断点。但什么也没发生。
    • @chaeusangchen 仔细阅读答案。调用点击事件前需要绑定ID参数。
    • 我已经在上面的代码中发布了一个ActionMethod。 @Mannan Bahelim
    • 直接点击actionlink能打断点吗?
    • 成功了。我漏了一个字,我很抱歉。但我想再问一件事。为什么ActionResult GetInsuranceCompany 中的参数string id 始终为空。我从 id = Model.PaymentCode 的那个操作链接中获取参数 id
    【解决方案2】:

    在新的 {} 部分中分配 onchange 事件,您可以在其中使用其 ID 引发特定操作链接的事件。

    @Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control", @id = "MyId", onchange = "MyFunction()" })
    
    <script type="text/javascript">
        function MyFunction() {
            //alert('Changed');
            document.getElementsByClassName("ddlSubmit").click();
            $('#YourLabelId').val('ReplaceWithThisValue');
        }
    </script>
    

    参考:
    Handling onchange event in HTML.DropDownList Razor MVC

    【讨论】:

    • 它不工作。如果单击 actionlink,我会在控制器中设置断点。但什么也没发生
    猜你喜欢
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多