dropdown list selected index changed event in mvc

dropdown list selected index changed event in mvc

When an item is selected in DropDownList, the JavaScript OnChange event handler will be raised which in turn will submit the Form, thus simulating the SelectedIndexChanged event in ASP.Net MVC Razor

<select class="select2 form-control mb-3 custom-select" 
name="ddl_Address" id="ddl_Address" onchange="GetCollegeAddress()">
     <option value="1">option 1</option>
      <option value="2">option 2</option>
      <option value="3">option 3</option>
      <option value="4">option 4</option>
</select>
<script>
    function GetCollegeAddress() {
        var CollegeId = $("#ddl_Address").val();
        alert(CollegeId);
        $.ajax({
            url: "GetAddress", //Your path should be here
            data: { CollegeId: CollegeId },
            type: "post",
            success: function (response) {
                $("#divBindAddress").html(response);
            }
        });
    }
</script>
public ActionResult GetAddress(string CollegeId)
        {
            List<dynamic> lst = new List<dynamic>();
            string message = "";
            try
            {
                if (Session["no"] != null)
                {
                    message = "hello";
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            catch (Exception ex)
            {
                message = "";
            }
           return Json(lst.ToList(), JsonRequestBehavior.AllowGet);
        }

2020-03-26