本文介绍了如何使用Knockout.Js将我的模型发布到Odata Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将模型发布到web api,每次只有一个项目出现在post方法中。就像我发布一个名为Movie的模型,Id和Title是这个模型的属性。

所以我的问题是当我试图使用knockout.js从客户端发布它时,只有标题值才会出现在web api post方法中。





在api中发布方法

Hi
i'm trying to post model to a web api where every time only one item in the model shown up at post method. like if i'm posting a model called Movie ,Id and Title are the properties of this model.
so here my problem is when i'm trying to post it from client side using knockout.js,only Title value is coming in the web api post method.


post method in api

[HttpPost]
        [ODataRoute("CreateMovie")]
        public IHttpActionResult CreateMovie(Movie parameters)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            string title = "Test";//parameters["Title"] as string;

            Movie movie = new Movie()
            {
                Title = title,
                ID = _db.Movies.Count + 1,
            };

            _db.Movies.Add(movie);

            return Created(movie);
        }



客户端代码:


client side code:

window.viewModel = (function () {
    var self = this;

    self.movies = ko.observableArray();
    self.id = ko.observable();
    self.title = ko.observable();
    self.year = ko.observable();
    self.duedate = ko.observable();
    self.ischeckedout = ko.observable();
    self.errorMessage = ko.observable();

    // Function to invoke the non-bindable "CreateMovie" action.
    self.createMovie = function () {
        var movieVal = { ID: self.id(), Title: self.title(), Year: self.year(), DueDate: null, IsCheckedOut: self.ischeckedout() };
        ajaxRequest("post", "odata/CreateMovie",movieVal)
            .done(function (result) {
                self.movies.push(new movie(result))
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                self.errorMessage(errorThrown);
            });
    }    // Ajax helper
    function ajaxRequest(type, url, data) {
        debugger;
        var options = {
            dataType: "json",
            contentType: "application/json",
            type: type,
            data: data ? ko.toJSON(data) : null
        };
        self.errorMessage(null); // clear error message
        return $.ajax(url, options);
    }
})();

$(document).ready(function () {
    ko.applyBindings(viewModel);
});







所以在web api中post methdo only标题每次都出现并非所有其他项目。



建议我这个。





提前致谢




So in web api post methdo only Title is showing up every time not all other items.

suggest me on this.


Thanks in advance

推荐答案








所以在web api post methdo中只有标题出现,而不是所有其他项目。



建议我这个。





提前致谢




So in web api post methdo only Title is showing up every time not all other items.

suggest me on this.


Thanks in advance


这篇关于如何使用Knockout.Js将我的模型发布到Odata Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:03