如何在控制器中的json中返回我的响应。 (Asp.net)

问题描述 投票:0回答:1

我将烤面包机用于表格通知。我总是收到错误消息,但数据已保存在数据库中。我认为该错误来自控制器,因为我没有以json形式发回。感谢您的帮助。

----- Controlleur:---------------

        public ActionResult CreateNewRentals(NewRentalDto newRental)
    {
        var hour = DateTime.Now;
        var customer = _context.Customers.Single(
            c => c.Id == newRental.CustomerId);

        var movies = _context.Movie.Where(
            c => newRental.MovieId.Contains(c.Id)).ToList();

        foreach (var movie in movies)
        {
            if (movie.NumberAvailable == 0)
               return BadRequest("le film est introuvable");

            movie.NumberAvailable--;
            var rental = new Rental
            {

                Customer = customer,
                Movie = movie,
                DateRented = DateTime.Now
            };

            _context.Rentals.Add(rental);

        }
        _context.SaveChanges();
        return Ok();
    }

---------------- La view --------------------

<script type="text/javascript">




    $(document).ready(function () {

        var vm = {
            movieId: []
        };
        var customers = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: customers,
            remote: {
                url: '/api/customers?query=%QUERY',
                wildcard: '%QUERY'
            }
        });

        $('#customer').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
        }, {
            name: 'customers',
            display: 'name',

            source: customers
        }).on("typeahead:select", function (e, customer) {
            vm.customerId = customer.id;



        });

        /* MOVIES */

        var movies = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: '/api/movies?query=%QUERY',
                wildcard: '%QUERY'
            }
        });
        $('#movie').typeahead({

            hint: true,
            highlight: true,
            minLength: 1
        }, {
            name: 'movies',
            display: 'name',
            source: movies
        }).on("typeahead:select", function (e, movie) {
            $("#movies").append("<li class='list-group-item'>" + movie.name + "</li>");
            $("#movie").typeahead("val", "");
            vm.movieId.push(movie.id);
        });
        $("#newRental").submit(function (e) {

            e.preventDefault();
            $.ajax({
                url: "/api/newRentals",
                method: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(vm),
                success: function (data) {
                    toastr.success(' successfully!');

                },
                error: function () {
                    toastr.error( 'error!');
                }
            });

        });
    });



</script>
javascript asp.net json asp.net-core typeahead.js
1个回答
0
投票

您没有将任何内容传递给Ok()结果。传递这样的值:返回Ok(在这里对象)或返回新的JsonResult(在这里对象)。区别在于Ok()返回HTTP状态200。

© www.soinside.com 2019 - 2024. All rights reserved.