0

I have one dropdown and one actionlink. where this actionlink will be clicked automatically when the dropdown changes. How to do that?. below is my code, thanks.

   @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"})

Controller

public ActionResult GetInsuranceCompany( ParamBlacklistPembayaranViewModel model,string id)
{ 
  LoadThirdPartyDDL(string.Empty, string.Empty, id);
  return View("Create", model);
}
chaeusang chen
  • 99
  • 1
  • 12

2 Answers2

2
 @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"})

You should call click event on drop down change like this:

 <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 : You want get updated PaymentCode. you have to inject url to pass PaymentCode on change event.

Mannan Bahelim
  • 1,289
  • 1
  • 11
  • 31
1

Assign onchange event in new {} section where you can raise the event of the particular action link by using their 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>

References:
Handling onchange event in HTML.DropDownList Razor MVC

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75