Below is the method in my controller:
public JsonResult GetRights(string ROLE_ID)
{
var result = db.APP_RIGHTS_TO_ROLES.Where(r => r.FK_ROLE_ID.ToString() == ROLE_ID).Select(r => r.APP_RIGHTS).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
The dropdownlist creation code:
@Html.DropDownList("ddlRoles", new SelectList(ViewBag.Roles, "ROLE_ID", "ROLE_NAME"),"--Select role--", new { onchange = "GetRights(this.value);" })
Lastly, a javascript snippet for handling dropdown item change:
<script type="text/javascript>
function GetRights(role_id) {
$.ajax({
type: "GET",
url: '@Url.Action("GetRights")',
dataType: "json",
data: { "ROLE_ID" : role_id.toString() },
success: successFunc,
error: errorFunc
})
}
function successFunc (data) {
alert("success");
if (data != null) {
var vData = data;
}
}
function errorFunc() {
alert("error");
};
</script>
Although the javascript function is triggered when changing selection, I am unable to navigate to the controller method. Can someone help me identify the issue? Thank you.