Skip to content

Instantly share code, notes, and snippets.

@tnayanam
Last active June 13, 2018 13:17
Show Gist options
  • Save tnayanam/173ab962fd0405e1808a0e500eefdef0 to your computer and use it in GitHub Desktop.
Save tnayanam/173ab962fd0405e1808a0e500eefdef0 to your computer and use it in GitHub Desktop.
// Suppose we want to update something based on Ajax Call
UI: HTML Dropdown
Step 1: // ***here we have the change event of dropdown which will call ajax
<select id="idselect" name="classselect" onchange="makeajaxcall()">
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Step 2: Make the actual ajax call on change of dropdown
<script>
function makeajaxcall()
{
//get the selected value from the dropdown
var idselect = $('#idselect').val()
$.ajax({
type: 'POST',
url: '/DropDown/hello', // THIS IS THE URL WHICH WILL BE CALLED UPON THE CONTROLLER
data: { id: idselect }, // HERE WE ARE SENDING THE JSON VALUE AS INPUT TO POST
success: function (data) {
//now here we are getting the json data, we will create a dropdown here dynamically
$("#idtest").html(markup); // Here bascailly add the result to the UI element which you want updated
},
error: function () {
// Error handling
}
});
}
</script>
Step 3: BACK END
[HttpPost]
public JsonResult hello(int id)
{
if (id == 2)
{
l1.Add(new SelectListItem { Text = "Dropdown value for Two", Value = "1" });
l1.Add(new SelectListItem { Text = "2A", Value = "2" });
}
var d = Json(l1);
return Json(l1); / sending data to the front end.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment