I am trying to populate CreditTobeTakenTextBox automatically after selecting a 'TeacherId' DropDownList item. I have Teacher Table in database where TeacherId is a primary key, and trying to retrieve CreditToBeTaken of a particular teacher. But it's not working. I have seen similar type of example in codeproject but they are not working for me. I think there is a problem in my ajax call. Please someone help me.
What I have tried:
Action:
public JsonResult CreditToBeTaken(int id)
{
db.Configuration.ProxyCreationEnabled = false;
string credit = "select CreditToBeTaken from Teacher where TeacherId='"+id+"'";
var creditsToBeTaken = db.Teachers.SqlQuery(credit);
return Json(creditsToBeTaken);
}
View:
Script File:
<script>
$(document).ready(function () {
$("#TeacherId").change(function () {
$.ajax({
url: '@Url.Action("CreditToBeTaken")',
type: 'POST',
dataType: 'json',
data: { teacherID: $("#TeacherId").val() },
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.success) {
// fill the Credit to be taken
$("#CreditToBeTakenTextBox").val(data.creditsToBeTaken);
}
else {
// show a message in a alert or div
alert('This Teacher ID is not valid. Try again!');
}
}
});
});
});
</script>