MVC 4 Changing a field based on DropDownListFor selection
First off, almost the same as the above question, but the solution isn\'t working for this one.
I have
Change your Javascript function with below fuction:
$("#CourseName").change(function () {
var courseID = $(this).val();
$.ajax({
type: 'POST',
url: '@Url.Action("FillCourseInfo","Student")', // we are calling json method
dataType: 'json',
data: { StudentID: @ViewBag.studentID, CourseID, courseID },
success: function (ret) {
$('#Dates').val(ret.courseDates);
$('#Project').val(ret.projectName);
$('#Graduated').val(ret.graduated);
},
error: function (ex) {
//alert('Failed to retrieve states.' + ex);
}
});
return false;
});
Change your controller function with below function
public JsonResult FillCourseInfo(int StudentID, int CourseID)
{
var ret = (from e in db.Enrollments
join c in db.Courses on e.CourseID equals c.CourseID
where e.StudentID == StudentID && e.CourseID == CourseID
select new StudentCourseDetails
{
courseDates = c.CourseStartDate.ToString() + " " + c.CourseEndDate.ToString(),
projectName = e.Project.ProjectTitle,
graduated = e.Graduated
});
return Json(ret);
}