简体   繁体   中英

ajax sending null to MVC Api Controller

I am trying to send the variable as vm from view to API Controller by Ajax but in controller the parameter of method that get it is null! the error is : An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Jquery Code :

            var vm = {
                courseIds: []
            };

            var courses = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: '/api/Course/GetCourses?query=%QUERY',
                    wildcard: '%QUERY'
                }
            });
            var teachers = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: '/api/Teacher/?query=%QUERY',
                    wildcard: '%QUERY'
                }
            });


            $('#teacher').typeahead({
                    minLength: 1,
                    highlight: true
                },
                {
                    name: 'teachers',
                    display: 'Name',
                    source: teachers
                }).on("typeahead:select",
                function(e, teacher) {
                    vm.teacherId = teacher.Id;
                });
 $("#courses").typeahead({
                    minLength: 1,
                    highlight: true
                },
                {
                    name: 'courses',
                    display: 'Name',
                    source: courses
                }).on("typeahead:select",
                function(e, course1) {
                    $("#courselist").append("<li class='list-group-item'>" + course1.Name + "</li>");
                    $("#courses").typeahead("val", "");
                    vm.courseIds.push(course1.Id);
                    
                    console.log(vm); 
                });
            $("#newPlan").submit(function(e) {
                e.preventDefault();
                $.ajax({
                        url: "/api/new/",
                        method: "post",
                        data: vm
                    }).done(function() {
                        console.log("Done!");
                    })
                    .fail(function() {

                    });
            });

and the Controller Code is :

        [HttpPost]
        public IHttpActionResult Plan(NewPlanDto coursePlan)
        {

            var teacher = _context.teachers.Single(m => m.Id == coursePlan.TeacherId);
            var courses = _context.Courses.Where(
                m => coursePlan.coursesId.Contains(m.Id)).ToList();


            if (courses != null)
            {
                foreach (var course in courses)
                {
                    var newCoursePlan = new CoursPlan()
                    {
                        Teacher = teacher,
                        Course = course

                    };
                    _context.CoursPlans.Add(newCoursePlan);
                }
            }
            _context.SaveChanges();

            return Ok();
        }

consule result :

courseIds: Array [ 4, 4 ] teacherId: 6

Your problem is that you do not serialize the object while sending it to your controller.

eg:

$.ajax({
        url: "/api/new/",
        method: "post",
        data: JSON.stringify(vm)
    }).done(function() {
        console.log("Done!");
    })
    .fail(function() {

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM