MVC 4 Changing multiple display fields based on DropDownListFor selection

前端 未结 2 647
半阙折子戏
半阙折子戏 2021-01-25 15:36

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

相关标签:
2条回答
  • 2021-01-25 16:01

    One thing you are doing is not returning a single result from your FillCourseInfo action.

    This means your json result is a list of StudentCourseDetails. You would have to use $('#Dates').val(data[0].courseDates); to get your values.

    Or if you're just expecting a single value you can use .FirstOrDefault() at the end of your linq query.

    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
    
                   }).FirstOrDefault();
    

    I created another .NET Fiddle for you. DotNetFiddle

    To use ToString in your linq query, convert the result to a list, then build your json

     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
                       {
                           courseStartDate = c.CourseStartDate,
                           courseEndDate = c.CourseEndDate,
                           projectName = e.Project.ProjectTitle,
                           graduated = e.Graduated
    
                       }).ToList()
                       .Select(a => new StudentCourseDetails() {
                           courseDates = a.courseStartDate.ToString() + " " + a.courseEndDate.ToString(),
                           projectName = a.projectName,
                           graduated = a.graduated
                       }).FirstOrDefault();
    
    0 讨论(0)
  • 2021-01-25 16:28

    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);
    }
    
    0 讨论(0)
提交回复
热议问题