1

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!

Talukder
  • 51
  • 2
  • 11
  • 1
    what does `data` look like when you set a breakpoint in the `success` function – GregH Jan 16 '19 at 18:26
  • I might be wrong, but as far as I know JavaScript is case sensitive. Try replacing Data with data. If that doesn't help, put breakpoints in the success function as GregH suggested. – Addy Jan 16 '19 at 20:58
  • Possible duplicate of [How to populate a cascading Dropdown with JQuery](https://stackoverflow.com/questions/18351921/how-to-populate-a-cascading-dropdown-with-jquery) – Sahil Sharma Jan 17 '19 at 06:59
  • solved by editing Data to data in ajax method,thanks @GregH – Talukder Jan 18 '19 at 17:34

0 Answers0