I have found some articles related to that but i did not find any cause of problems. i want to select a field from dropdownlist and populate another field based on selection from dropdownlist. Here is code for jsonresult
public JsonResult GetStudentbyStudentId(int studentId)
{
var data = db.Students.Select(n => new { StudentID = n.StudentId,
Name = n.Name,Email=n.Email });
var student = data.ToList().Find(x => x.StudentID == studentId);
return Json(student, JsonRequestBehavior.AllowGet);
}
Here is the code for jquery ajax
$(document).ready(function(){
$("#StudentId").change(function(){
var a= $("#StudentId").val();
$("#Name").val("");
$("#Email").val("");
var json={StudentId:a} ;
$.ajax({
type: "POST",
url: '@Url.Action("GetStudentbyStudentId", "Student")',
contentType: "application/json;charset=utf-8",
Data: JSON.stringify(json),
success: function (data) {
$("#Name").val("" + data.Name);
$("#Email").val("" + data.Email);
}
});
})
})
Thanks in advance!