簡體   English   中英

ajax 向 MVC Api 控制器發送空值

[英]ajax sending null to MVC Api Controller

我試圖通過 Ajax 將變量作為 vm 從視圖發送到 API 控制器,但在控制器中,獲取它的方法的參數為空! 錯誤是:EntityFramework.SqlServer.dll 中發生了“System.NotSupportedException”類型的異常,但未在用戶代碼中處理

查詢代碼:

            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() {

                    });
            });

控制器代碼是:

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

領事結果:

courseIds: Array [ 4, 4 ] TeacherId: 6

您的問題是您在將對象發送到控制器時沒有對其進行序列化。

例如:

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

    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM